Java 泽西岛2号&x2B;Spring:@Autowired为空
在本文的帮助下,我尝试将Jersey 2与Spring结合使用: 但当应用程序在客户端请求后尝试调用AutowiredBean时,AutowiredBean为null。 在applicationContext.xml中,我只有组件扫描设置Java 泽西岛2号&x2B;Spring:@Autowired为空,java,spring,jersey-2.0,Java,Spring,Jersey 2.0,在本文的帮助下,我尝试将Jersey 2与Spring结合使用: 但当应用程序在客户端请求后尝试调用AutowiredBean时,AutowiredBean为null。 在applicationContext.xml中,我只有组件扫描设置 In pom.xml: <spring.version>4.1.0.RELEASE</spring.version> <jersey.version>2.12</jersey.version> @Compo
In pom.xml:
<spring.version>4.1.0.RELEASE</spring.version>
<jersey.version>2.12</jersey.version>
@Component
@RequestScoped
@Path("/user")
public class UserREST {
@Autowired
private UserFacade userFacade;
@POST
@Path("/auth")
@Consumes(MediaType.APPLICATION_JSON)
@Produces({MediaType.APPLICATION_JSON})
public AuthResponse authorize(User user){
return userFacade.authorize(user); // Null is caught here
}
}
我做错了什么
UPD:
这里是我的pom.xmlSpring托管bean不能直接注入JAX-RS类,您需要使用Jersey扩展将其与Spring集成 在
pom.xml
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>2.12</version>
</dependency>
org.glassfish.jersey.ext
在页面底部,有一个指向Github项目的链接
我在您的项目中看到的另一个问题是,您没有说明应该如何加载和配置spring上下文。您需要在web.xml中配置它
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
org.springframework.web.context.ContextLoaderListener
上下文配置位置
类路径:applicationContext.xml
如果您使用基于java的方法进行spring配置,您还需要:
servletContext.setInitParameter("contextConfigLocation", "<NONE>");
servletContext.setInitParameter(“contextConfigLocation”,即“”);
在您的WebApplicationInitializer
实现中是否使用组件扫描扫描扫描正确的包?@yate组件扫描被正确设置为根包(始终对我有效)@luiggi这不是重复。请证明或留下反馈。发布您所做的配置和其他相关信息以复制此问题。我不知道为什么它被标记为“离题”。但它可以被认为是“不清楚你在问什么”,因为这个问题并没有很好的问题背景。
servletContext.setInitParameter("contextConfigLocation", "<NONE>");