spring @Autowire注释不适用于resteasy类

时间:2016-11-24 04:15:21

标签: spring wildfly resteasy

我使用resteasy 3.0.9 RESTful Web服务以及使用wildfly 10作为应用程序服务器的jsf 2.2和spring 4.3框架集成应用程序,我无法将spring bean注入restful类。以下是申请中的配置。

  in web.xml 

  <context-param>
    <param-name>contextClass</param-name>
    <param-value>
        org.springframework.web.context.support.AnnotationConfigWebApplicationContext
    </param-value>
</context-param>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>com.test.config.AppConfig</param-value>
</context-param>

<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
</servlet-mapping>


  <listener>
     <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
  </listener>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
 <servlet>
   <servlet-name>resteasy</servlet-name>
  <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
 </servlet>

 <servlet-mapping>
   <servlet-name>resteasy</servlet-name>
   <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>

  <context-param>
     <param-name>resteasy.servlet.mapping.prefix</param-name>
    <param-value>/rest</param-value>
   </context-param>

   <context-param>
    <param-name>resteasy.resources</param-name>
    <param-value>com.test.rest.PrintService</param-value>
   </context-param>

我的服务类

    @Path("/customer") @Component
    public class PrintService {

    @Autowired // not working allways gives nullpointerexception 
    CustomerBo customerBo;

    @GET
    @Path("/print")
    public Response printMessage() {

    //      customerBo = (CustomerBo) SpringApplicationContext.getBean("customerBo"); here bean is getting injected

        String result = customerBo.getMsg();

        return Response.status(200).entity(result).build();

    }

    }

@Component
public class CustomerBo {

    @PersistenceContext
    EntityManager entityManager;

    @Autowired
    Bean2 bean2;

    public String getMsg() {

        entityManager.createQuery("Select * from employee e"); // nullpointerException
        return "RESTEasy + Spring example" + bean2.msgFromBean2(); //nullpointerException

}

在pom.xml中

    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-spring</artifactId>
        <version>3.0.9.Final</version>
    </dependency>

1 个答案:

答案 0 :(得分:1)

REST轻松运行自己的上下文而不是Spring应用程序的春天,获取JAX-RS @Context以获取ServletContext。然后,您将能够获得spring应用程序上下文

@Path("/customer") @Component
public class PrintService {

@Context ServletContext servletContext; 

@GET
@Path("/print")
public Response printMessage() {

ApplicationContext ctx =
                 WebApplicationContextUtils.getWebApplicationContext(servletContext);

customerBo = (CustomerBo) customerBo= ctx.getBean("customerBo",CustomerBo.class);

    String result = customerBo.getMsg();

    return Response.status(200).entity(result).build();

}

}
相关问题