java 如何从 JAX-WS Web 服务中访问 ApplicationContext?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/602319/
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-29 13:02:28  来源:igfitidea点击:

How can I access the ApplicationContext from within a JAX-WS web service?

javaweb-servicesspringjax-ws

提问by pihentagy

Similar to How can I access the ServletContext from within a JAX-WS web service?, is there a way to access applicationContext, easier than this?

类似于如何从 JAX-WS Web 服务中访问 ServletContext?,有没有办法访问applicationContext,比这更容易?

import javax.annotation.Resource;
import javax.jws.WebService;
import javax.servlet.ServletContext;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

@WebService
public class MyWebService {
    // boilerplate code begins :(

    @Resource
    private WebServiceContext context;
    private WebApplicationContext webApplicationContext = null;

    /**
     * @return
     * @throws IllegalStateException
     */
    private WebApplicationContext getWebApplicationContext()
            throws IllegalStateException {
        if (webApplicationContext != null)
            return webApplicationContext;
        ServletContext servletContext =
                (ServletContext) context.getMessageContext().get(
                        MessageContext.SERVLET_CONTEXT);
        webApplicationContext =
                WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
        return webApplicationContext;
    }
}

采纳答案by duffymo

I don't think that the web service should have to know about web or servlet contexts or its application context. I don't see why it should have to know any of that. Shouldn't it be far more passive? Inject what it needs and let it do its work. The service interactions with a client should be based on a contract defined up front. If it has to get unknown values from a context of some kind, how will clients know what needs to be set or how to set it?

我认为 Web 服务不必知道 Web 或 servlet 上下文或其应用程序上下文。我不明白为什么它必须知道任何这些。它不应该更被动吗?注入它需要的东西,让它做它的工作。与客户端的服务交互应该基于预先定义的契约。如果它必须从某种上下文中获取未知值,客户端如何知道需要设置什么或如何设置?

I'd go further and say that a web service should be a wrapper for a Spring service interface. It's just one more choice among all the possible ways to expose it. Your web service should do little more than marshal and unmarshal the XML request/response objects and collaborate with Spring services.

我会更进一步说,Web 服务应该是 Spring 服务接口的包装器。这只是暴露它的所有可能方式中的另一种选择。您的 Web 服务应该只对 XML 请求/响应对象进行编组和解组,并与 Spring 服务协作。

回答by maximdim

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;


@WebService( 
    endpointInterface = "Bla", 
    targetNamespace = "http://bla/v001", 
    wsdlLocation = "WEB-INF/wsdl/bla.wsdl",    
    serviceName = "BlaService",
    portName = "BlaPort")
public class BlaWs extends SpringBeanAutowiringSupport implements BlaPort {

  @Autowired
  @Qualifier("dao") 
  private Dao dao;
  ...
}

回答by Michael Wiles

Make your web service bean extend a spring bean.

使您的 Web 服务 bean 扩展一个 spring bean。

like this

这样

回答by Maurice Perry

I would install a Filter that saves ServletContext before chaining in a ThreadLocal

我会安装一个过滤器,在链接到 ThreadLocal 之前保存 ServletContext

回答by user1955401

According to the JavaDoc for the SpringBeanAutowiringSupport class, see: http://docs.spring.io/autorepo/docs/spring-framework/3.0.x/api/org/springframework/web/context/support/SpringBeanAutowiringSupport.html

根据 SpringBeanAutowiringSupport 类的 JavaDoc,请参阅:http://docs.spring.io/autorepo/docs/spring-framework/3.0.x/api/org/springframework/web/context/support/SpringBeanAutowiringSupport.html

Read the NOTE: at the end of the javadoc.

阅读注意:在 javadoc 的末尾。

The original question, may in fact, be the way that this should be implemented.

最初的问题实际上可能是应该如何实施。