8

我有一个 Metro jax-ws 网络服务,看起来或多或少像这样:

@WebService
@Transactional
public class UserManagementServiceImpl {

    @Resource
    private WebServiceContext context;

    ...
}

始终为空WebServiceContext。但是,如果我删除@TransactionalWebServiceContext 被注入。

有人知道解决方法吗?

谢谢。

4

3 回答 3

5

我找到了解决方法。使用 setter 注入代替字段注入:

@WebService
@Transactional
public class UserManagementServiceImpl {

    private WebServiceContext context;

    @Resource
    public void setContext(WebServiceContext context) {
        this.context = context;
    }
    ...
}
于 2011-04-28T20:37:02.657 回答
3

Web 服务和事务管理的问题在于,每个都创建了一个类的代理,而第二个创建代理的并没有得到真正的实现,而是得到了代理(事情往南走)。

避免这种情况的方法是将 Web 服务端点实现的所有调用委托给服务。所以你需要两个具体的类:S。

我不知道这是否是最好的方法,但这是我发现的最好的方法。

而且它可能会稍微清理一下代码,因为看起来用户管理器关心 web 服务,这看起来不正确。

于 2011-04-28T15:35:11.553 回答
2

我怀疑在处理对 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();
于 2014-07-15T10:50:02.980 回答