如何将@Resource WebServiceContext注入与Spring的@Transactional一起使用
我有一个 Metro jax-ws web 服务,看起来或多或少像这样:
@WebService
@Transactional
public class UserManagementServiceImpl {
@Resource
private WebServiceContext context;
...
}
WebServiceContext
始终为 null。但是,如果我删除 @Transactional
则会注入 WebServiceContext。
有人知道解决方法吗?
谢谢。
I hava a Metro jax-ws webservice that looks more or less like this:
@WebService
@Transactional
public class UserManagementServiceImpl {
@Resource
private WebServiceContext context;
...
}
The WebServiceContext
is allways null. However, if I remove @Transactional
the WebServiceContext is injected.
Anybody knows a workaround?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我找到了解决方法。使用 setter 注入代替字段注入:
I've found a workaround. Use setter injection instead of field injection:
Web 服务和事务管理的问题在于,每个服务都创建该类的代理,而第二个创建代理的代理并没有获得真正的实现,而是获得了代理(并且事情进展顺利)。
避免这种情况的方法是将来自 Web 服务端点实现的所有调用委托给服务。所以你需要两个具体的类:S。
我不知道这是否是最好的方法,但这是我发现的最好的方法。
而且它可能会稍微清理一下代码,因为看起来用户管理器关心网络服务,这看起来不太正确。
The problem with webservices and the transaction management is that each creates a proxy of the class, and the 2nd one to create a proxy doesn't get the real implementation but the proxy (and things go south).
The way to avoid this is to delegate all the calls from the webservice endpoint implementation to the service. So you'll need two concrete classes :S.
I don't know if this is the best way to do it, but it's the best I've found.
And it might clean up the code a bit, as it looks like the User Manager cares about webservices, which doesn't look right.
我怀疑这可能会在处理对 Web 服务的同时调用时导致问题,因为 Servlet 是单例,所有实例数据都由所有线程“共享” - 因此,即使您在仍在忙于之前的电话。也许像
I suspect this may cause problems when handling simultaneous calls to the web service since the Servlet is a singleton, all instance data is "shared" by all threads - so your "private context" will keep being overridden by the next call even while you are still busy with a previous call. Maybe something like