Spring / Jersey每次拨打休息时都会创建新的服务对象

时间:2012-10-12 21:40:21

标签: spring rest jersey

每次我进行休息呼叫localhost:8080 / user / 1时,它会创建一个新的用户服务对象,而不是使用在服务器启动期间创建的用户服务对象。我在使用Jersey和Spring。

的applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd"
    default-autowire="byName"
    >

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
 <context:annotation-config/>
<context:component-scan base-package="com.gigenginecore.platform.dataaccess.impl" /> 
    <context:component-scan base-package="com.gigengine.dataaccess"/>
    <context:component-scan base-package="com.gigengine.dataaccess.impl"/> 
    <context:component-scan base-package="com.gigenginecore.platform.service" /> 
    <context:component-scan base-package="com.gigengine.service" />  
    <context:component-scan base-package="com.gigenginecore.provider" />   

</beans>

///// UserService类的代码片段,我甚至试图强制范围为单例。

@Service 
@Scope("singleton") 
@Path("/user")
public class UserService extends AbstractUserService {


///from my web.xml
<servlet>
        <servlet-name>jersey-servlet</servlet-name>
         <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
         <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>   
           <param-value>com.gigengine.service,com.gigenginecore.platform.service, com.gigenginecore.provider</param-value> 
        </init-param> 
        <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
        </init-param> 
        <load-on-startup>1</load-on-startup>
    </servlet> 
    <servlet-mapping>
        <servlet-name>jersey-servlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

///// 所有DAO都会在启动时正确实例化并注入到用户服务类中,但是当我调用服务端点本身时,它会创建一个新的UserService类,其中DAO为null。

我需要做些什么来抓住在启动时创建的那个?不确定为什么它在创建一个“Spring”单例时会创建一个新实例。

有什么建议吗?

1 个答案:

答案 0 :(得分:4)

使用Jersey with Spring时,请使用com.sun.jersey.spi.spring.container.servlet.SpringServlet代替com.sun.jersey.spi.container.servlet.ServletContainer。见http://jersey.java.net/nonav/apidocs/latest/jersey/contribs/jersey-spring/com/sun/jersey/spi/spring/container/servlet/package-summary.html