gpt4 book ai didi

java - Spring bean 启动时为 null

转载 作者:行者123 更新时间:2023-12-02 04:30:35 26 4
gpt4 key购买 nike

我正在尝试自动连接一个bean,但当我尝试这样做时它总是为空。无论我如何尝试(自动或构造函数/属性注入(inject)),它始终为空。

请问我做错了什么?

package com.ricki.relocate.service;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import org.springframework.beans.factory.annotation.Autowired;

import com.ricki.relocate.database.RelocateServiceDaoImpl;

@Path("/service")
public class Service {

@Autowired
private RelocateServiceDaoImpl service;

@Path("/createUser")
@GET
@Produces("application/json")
public String createUser(@QueryParam("firstname") String firstname, @QueryParam("lastname") String lastname, @QueryParam("username") String username, @QueryParam("password") String password) {
boolean result = service.createNewUser(firstname, lastname, username, password);
return Boolean.toString(result);
}
}


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ">

<!-- Initialization for data source -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/Relocate" />
<property name="username" value="" />
<property name="password" value="" />
</bean>

<!-- Definition for RelocateServiceDaoImpl bean -->
<bean id="relocateServiceDaoImpl" class="com.ricki.relocate.database.RelocateServiceDaoImpl">
<constructor-arg index="0" ref="dataSource" />
</bean>

<!-- Definition for service bean which uses dao bean -->
<bean id="service" class="com.ricki.relocate.service.Service">
<property name="relocateServiceDao" ref="relocateServiceDaoImpl" />
</bean>

</beans>




<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>CrunchifyRESTJerseyExample</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/relocate/*</url-pattern>
</servlet-mapping>
</web-app>

最佳答案

主要问题是 Spring 对您的 com.ricki.relocate.service.Service 类一无所知 - 它根本不存在于 Spring 上下文中。

您应该使用@Component注释来注释它,以便Spring能够发现它并执行 Autowiring ,并且您还应该将Jersey Servlet类更改为com.sun.jersey.spi。 spring.container.servlet.SpringServlet。这应该可行。

尝试查看本教程: http://www.mkyong.com/webservices/jax-rs/jersey-spring-integration-example/

关于java - Spring bean 启动时为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31526758/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com