2015-07-20 74 views
0

我试图自动连线一个bean,当我尝试这样做时它总是空的。无论我如何尝试(自动或构造函数/属性注入)它总是空。启动时的Spring bean null

我该做什么不正确的事?

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> 
+2

Spring不会自动的注入新泽西州的字段/方法/构造函数(或任何JAXRS实现,这是)管理对象。你有没有建立两者的适当整合? –

+0

不,我没有 - 我正在按照我现在已经偏离的教程 – Biscuit128

+2

这可能是问题所在。寻找Spring-Jersey桥。 –

回答

2

的主要问题是,Spring知道也不关心你的com.ricki.relocate.service.Service类 - 它根本没有在Spring上下文存在。

您应该使用@Component注释对其进行注释,以便Spring能够发现它并执行自动布线,并且还应该将Jersey Servlet类更改为com.sun.jersey.spi.spring.container.servlet.SpringServlet。这应该工作。

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

相关问题