繁体   English   中英

Spring bean启动时为空

[英]Spring bean null on startup

我正在尝试自动连线一个bean,当我尝试这样做时,它始终为null。 无论我如何尝试(自动或构造函数/属性注入),它始终为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>

主要问题是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/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM