java 如何在 Spring 的 @Transactional 中使用 @Resource WebServiceContext 注入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5820969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:56:49  来源:igfitidea点击:

How to use @Resource WebServiceContext injection with Spring's @Transactional

javaspringjax-wsjava-metro-framework

提问by adrianboimvaser

I hava a Metro jax-ws webservice that looks more or less like this:

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

@WebService
@Transactional
public class UserManagementServiceImpl {

    @Resource
    private WebServiceContext context;

    ...
}

The WebServiceContextis allways null. However, if I remove @Transactionalthe WebServiceContext is injected.

WebServiceContext是百达空。但是,如果我删除@Transactional了注入的 WebServiceContext。

Anybody knows a workaround?

有人知道解决方法吗?

Thanks.

谢谢。

采纳答案by adrianboimvaser

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

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

@WebService
@Transactional
public class UserManagementServiceImpl {

    private WebServiceContext context;

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

回答by Augusto

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).

webservices 和事务管理的问题在于,每个服务都创建了一个类的代理,而第二个创建代理的不是真正的实现,而是代理(并且事情向南)。

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.

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

I don't know if this is the best way to do it, but it's the best I've found.

我不知道这是否是最好的方法,但这是我找到的最好的方法。

Andit might clean up the code a bit, as it looks like the User Manager cares about webservices, which doesn't look right.

而且它可能会清理一下代码,因为它看起来像关于web服务的用户管理的烦恼,不右看看。

回答by ian visser

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

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