2016-11-24 5 views
1

Я использую resteasy 3.0.9 Веб-службы RESTful вместе с интегрированным приложением jsf 2.2 и весной 4.3 с использованием wildfly 10 в качестве сервера приложений, я не мог вставлять весенний боб в успокоительный класс. ниже приведена конфигурация в приложении.spring @ Аннотации автозапуска не работают в классе resteasy

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

REST легко работает на собственном контексте, чем весной яровой приложения, получить JAX-RS @Context, чтобы получить ServletContext. Тогда вы могли бы получить контекст весеннего внесения

@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(); 

} 

} 
+0

переменные autowired внутри customerBo также нулевое напр: @peristenceContext EntityManager это ВСЕГДА дает мне пустой – kim

+0

вы можете обновить ваш customerBo боб определение – kuhajeyan

+0

добавлено определение customerBo – kim