1

我有一个使用 Spring 的 Jersey 应用程序我正在使用@InjectParam希望(以及文档中写的内容)Jersey 会从 Spring Container 获取对象,但似乎 Jersey 是在创建对象而不是向 Spring 请求它.

有没有办法检查 Spring IOC 是否在 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> 

我的班级提供对工厂的基本访问,但工厂是新的

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

我已经添加了@Component,但仍然没有。

4

1 回答 1

1

使用了错误的 servlet。它应该是

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

当前使用 Jersey 自己的容器,它只会使用默认构造函数创建对象。Spring Servlet 允许 Jersey 附加到 Spring Container。

于 2012-10-15T08:31:17.893 回答