如何将@Resource WebServiceContext注入与Spring的@Transactional一起使用

发布于 2024-11-04 02:12:18 字数 343 浏览 6 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷情 2024-11-11 02:12:18

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

@WebService
@Transactional
public class UserManagementServiceImpl {

    private WebServiceContext context;

    @Resource
    public void setContext(WebServiceContext context) {
        this.context = context;
    }
    ...
}

I've found a workaround. Use setter injection instead of field injection:

@WebService
@Transactional
public class UserManagementServiceImpl {

    private WebServiceContext context;

    @Resource
    public void setContext(WebServiceContext context) {
        this.context = context;
    }
    ...
}
や三分注定 2024-11-11 02:12:18

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.

红颜悴 2024-11-11 02:12:18

我怀疑这可能会在处理对 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();

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

ThreadLocal<WebServiceContext> WSS = new ThreadLocal<WebServiceContext>();

@Resource
public void setContext(WebServiceContext context) {
    WSS.set(context);
}

// then where you need the context use WSS.get();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文