JBoss 6 + Spring 3.0.5 + JAX-WS/CXF
We've got our project running on JBoss 6 with Spring 3.0.5. Everything went smooth until we tried to implement some Web Services with JAX-WS. If I try doing some simple WS (like adding 2 numbers), it just works - I put annotations and add annotated class as a servlet. But things are getting harder if I try to have my JAX-WS classes populated with dependencies.
Here's my code:
@WebService(name = "principal")
public class PrincipalWebService extends SpringBeanAutowiringSupport {
@Autowired
private PrincipalManager manager;
@WebMethod
public int add(int a, int b) {
return a + b;
}
@WebMethod
public Principal getById(int i) {
return manager.getById(i);
}
}
Add method works, but getById fails with NPE. I've been debugging SpringBeanAutowiringSupport and it looks like ContextLoader.getCurrentWebApplicationContext() returns null. It means SpringBeanAutowiringSupport constructor is being called before context is initialized.
I've been trying following CXF instructions on running app with Spring. I don't have this code right now, but I've registered PrincipalWebService as a bean, created spring file to configure CXF and added this bean via it's ID as an endpoint. It went good on Jetty, but failed on JBoss. I've been receiving different kinds of exceptio开发者_如何学JAVAns depending on how I configure CXF, but root cause was the same - JBoss 6 CXF version is compiled against Spring 2.5, so we had library inconsistency.
Does anyone has any ideas of getting IoC working for Jax-ws services on JBoss 6?
Ok, I've found a workaround. All we need to do is to move dependency injection to @PostConstruct method:
@PostConstruct
public void init() {
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}
You should register your endpoint in your spring context using jaxws:endpoint, and refer to your bean using the implementor attribute. For example:
<jaxws:endpoint id="NotificationImpl"
implementorClass="com.foo.ws.notification.NotificationImpl"
implementor="#notificationImpl"
serviceName="notification:Notification"
address="/notification"
xmlns:notification="http://notification.ws.foo.com">
Endpoint implementation:
@Component("notificationImpl")
@WebService(endpointInterface="com.foo.ws.notification.Notification")
public class NotificationImpl implements Notification {
@Autowired MessagingService messagingService = null;
//...
}
I hade same problem but on WebLogic and I have solved it as described here https://jira.springsource.org/browse/SPR-5652 .
精彩评论