泽西2 +春天:@Autowired是空的

Rey*_*ard 4 java spring jersey-2.0

我试图在本文的帮助下使用Jersey 2和Spring: 如何在Spring IoC容器中使用Jersey 2

但是当应用程序尝试在客户端请求之后调用它时,autowired bean为null.在applicationContext.xml中,我只有组件扫描设置.

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
    }
}
Run Code Online (Sandbox Code Playgroud)

-

@Component
public class UserFacade {

    public AuthResponse authorize(com.pushock.model.User user){
        AuthResponse response = new AuthResponse();
        response.setAuthorized(true);
        return response;
    }
}
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

UPD: 这是我的pom.xml https://bitbucket.org/spukhov/memo-ws/src/00724e00e3aa786f62fd0e43fe0606de6ae569df/pom.xml?at=master

Õzb*_*bek 11

Spring管理的bean不能直接注入JAX-RS类,你需要使用Jersey扩展来将它与Spring集成.

你有一个maven依赖,你没有 pom.xml

<dependency>
    <groupId>org.glassfish.jersey.ext</groupId>
    <artifactId>jersey-spring3</artifactId>
    <version>2.12</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

请参阅Jersey文档:第22章Spring DI,在页面底部,有一个示例spring集成 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>
Run Code Online (Sandbox Code Playgroud)