Estoy escribiendo un servicio RESTful (usando CXF en JBoss) en el que he inyectado otra clase usando Spring (Autowired). Pero la clase no se inyecta y es nula.Spring Bean no inyectado en el servicio web CXF, ¿Por qué?
interfaz de servicios Web y clase (¿Dónde inyección tiene que suceder)
package com.company.project.web;
@Path("/myws")
public interface IMyWebService {
@POST
@Path("/doSomething")
@Consumes("application/json")
@Produces("application/json")
MyResponse doSomething(MyRequest myRequest)
}
@Service("myWebService")
public class MyWebService implements IMyWebService {
@Autowired
private IMyCore myCore;
public MyResponse doSomething(MyRequest myRequest) {
....
}
}
Lo que tiene que ser inyectado
package com.company.project.biz;
public interface IMyCore {
MyResponse doSomething(MyRequest myRequest);
}
@Component("myCore")
public class MyCore implements IMyCore {
public MyResponse doSomething(MyRequest myRequest) {
.....
}
}
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-http.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<context:annotation-config />
<context:component-scan base-package="com.company.project"/>
<jaxrs:server id="myWebService" address="/">
<jaxrs:serviceBeans>
<bean class="com.company.project.web.MyWebService" />
</jaxrs:serviceBeans>
<jaxrs:extensionMappings>
<entry key="json" value="application/json" />
</jaxrs:extensionMappings>
</jaxrs:server>
</beans>
Mi se rvice está activo (http://localhost:8080/{warname}/myws/doSomething) pero la instancia MyCore no se está inyectando en MyWebService (en el campo myCore). Siempre es nulo y mi servicio no funciona como se esperaba, en su lugar lanza NullPointerException
Intenté todas las entradas recopiladas en google. ¡Sin suerte! Tu ayuda es altamente apreciada.
Saludos
He intentado esto y no funciona para mí. Parece que el servicio web no es creado por Spring y, por lo tanto, no se está autoconectando. –