How to use @Resource WebServiceContext injection using Spring @Transactional

I am using the Metro jax-ws web service, which looks something like this:

@WebService @Transactional public class UserManagementServiceImpl { @Resource private WebServiceContext context; ... } 

WebServiceContext always null. However, if I delete @Transactional , then WebServiceContext is entered.

Does anyone know a workaround?

Thanks.

+6
source share
3 answers

I found a workaround. Use installer injection instead of field injection:

 @WebService @Transactional public class UserManagementServiceImpl { private WebServiceContext context; @Resource public void setContext(WebServiceContext context) { this.context = context; } ... } 
+4
source

The problem with web services and transaction management is that everyone creates a class proxy, and the second one does not get a real implementation to create a proxy server, but a proxy (and everything goes south).

A way to avoid this is to transfer all calls from the webservice endpoint implementation to the service. So, you will need two specific classes: S.

I don't know if this is the best for this, but it is the best I have found.

And it can clear up the code a bit, because it looks like User Manager takes care of web services, which doesn’t look like that.

+2
source

I suspect this may cause problems while accessing the web service at the same time, since Servlet is singleton, all instance data is “shared” by all threads, so your “private context” will remain overridden by the next call while you are still busy with the previous call. Maybe something like

 ThreadLocal<WebServiceContext> WSS = new ThreadLocal<WebServiceContext>(); @Resource public void setContext(WebServiceContext context) { WSS.set(context); } // then where you need the context use WSS.get(); 
+1
source

Source: https://habr.com/ru/post/886893/


All Articles