【问题标题】:How to use @Resource WebServiceContext injection with Spring's @Transactional如何在 Spring 的 @Transactional 中使用 @Resource WebServiceContext 注入
【发布时间】:2011-08-14 19:49:10
【问题描述】:
我有一个 Metro jax-ws 网络服务,看起来或多或少像这样:
@WebService
@Transactional
public class UserManagementServiceImpl {
@Resource
private WebServiceContext context;
...
}
WebServiceContext 始终为空。但是,如果我删除 @Transactional,则会注入 WebServiceContext。
有人知道解决方法吗?
谢谢。
【问题讨论】:
标签:
java
spring
jax-ws
java-metro-framework
【解决方案1】:
我怀疑这在处理对 Web 服务的同时调用时可能会导致问题,因为 Servlet 是单例的,所有实例数据都是由所有线程“共享”的 - 所以你的“私有上下文”将继续被下一次调用覆盖当您还在忙于上一个电话时。也许像
ThreadLocal<WebServiceContext> WSS = new ThreadLocal<WebServiceContext>();
@Resource
public void setContext(WebServiceContext context) {
WSS.set(context);
}
// then where you need the context use WSS.get();
【解决方案2】:
我找到了解决方法。使用 setter 注入代替字段注入:
@WebService
@Transactional
public class UserManagementServiceImpl {
private WebServiceContext context;
@Resource
public void setContext(WebServiceContext context) {
this.context = context;
}
...
}
【解决方案3】:
webservices 和事务管理的问题在于,每个都创建了一个类的代理,而第二个创建代理的并没有得到真正的实现,而是得到了代理(事情往南了)。
避免这种情况的方法是将来自 web 服务端点实现的所有调用委托给服务。所以你需要两个具体的类:S.
我不知道这是否是最好的方法,但这是我找到的最好的方法。
而且它可能会稍微清理一下代码,因为看起来用户管理器关心 web 服务,这看起来不太对。