我正在尝试使用Jersey和Spring。但是对于我的生活,我无法弄清楚为什么我的Spring依赖关系没有注入我的Rest类。
每次我尝试调用自动连接的依赖项时,我都会得到NULL。任何人都可以建议为什么我的依赖没有被注射?
My Rest Class看起来像这样:
com.myapp.rest
@Component
@Scope("request")
@Path("/home")
public class ChartResource {
@Autowired
ChartService chartService;
@GET
@Path("/chart")
@Produces(APPLICATION_JSON)
public Bean getChart() {
return chartService.retrieveChart();
}
我的web.xml文件如下所示
<servlet>
<servlet-name>myapp</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>org.codehaus.jackson.jaxrs;com.myapp.rest</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>org.codehaus.jackson.jaxrs</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
我的applicationContext.xml是标准的,它指定了组件扫描的基础包:
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<annotation-driven />
<resources mapping="/resources/**" location="/resources/" />
<context:component-scan base-package="com.myapp" />
</beans:beans>
答案 0 :(得分:2)
尝试添加
<context:annotation-config />
我不确定你需要让Jersey的注释工作,但@Autowired
是一个Spring注释,所以你需要使用Spring版本的<annotation-config>
才能获得要正常工作。
答案 1 :(得分:1)
好的 - 愚蠢的问题。我的applicationContext没有被加载,所以它没有拿起我为组件扫描定义的包!