Jersey @InjectParam creating a new Object instead of taking from Spring

自闭症网瘾萝莉.ら 提交于 2019-12-24 09:17:06

问题


I have a Jersey Application using Spring I am using the @InjectParam in the hope (and what is written in the documentation) that Jersey would obtain the object from the Spring Container, but it seems Jersey is creating the object instead of asking Spring for it.

Is there a way of checking if the Spring IOC is registered with Jersey ?

web.xml

<?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_2_5.xsd"
version="2.5">
<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:appContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<servlet>
    <servlet-name>SpringDispatcher</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>SpringDispatcher</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app> 

My class provide basic access to the factory but the factory is new

public FileStoreService(@InjectParam("dataAccessFactory") factory ) {  
   this.factory = factory;    
} 

and i have added the @Component and still nothing.


回答1:


The wrong servlet was being used. It should be

<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>

The current one uses Jersey own container, which will only create objects using the default constructer. The Spring Servlet allows Jersey to attached to the Spring Container.



来源:https://stackoverflow.com/questions/12857390/jersey-injectparam-creating-a-new-object-instead-of-taking-from-spring

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!