Java 如何从JAX-WS web服务中访问ApplicationContext?

Java 如何从JAX-WS web服务中访问ApplicationContext?,java,web-services,spring,jax-ws,Java,Web Services,Spring,Jax Ws,与之类似,是否有比此更容易访问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.we

与之类似,是否有比此更容易访问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;
    }
}

我将安装一个过滤器,在链接到ThreadLocal之前保存ServletContext。我认为web服务不必了解web或servlet上下文或其应用程序上下文。我不明白它为什么要知道这些。它不应该更被动吗?注入它需要的东西,让它完成它的工作。与客户机的服务交互应该基于预先定义的契约。如果必须从某种上下文中获取未知值,客户机如何知道需要设置什么或如何设置


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

使您的web服务bean扩展Spring bean


与SpringBeanAuthoringSupport类的JavaDoc类似,请参见:

阅读javadoc末尾的注释


事实上,最初的问题可能是实现的方式。

那么,如果我不能说:appContext.getBean('myBean'),那么我如何与Spring服务协作?通过setter或构造函数将其注入。依赖项注入意味着“不要打电话给我们;我们会打电话给你。”你的对象不需要有应用程序上下文才能得到它们所需要的。你不能。如果我在glassfish下测试我的web服务,将创建一个新的web服务,但未对其进行配置:-o这是一个为期一天的调试,以获得此知识:(我正在编写我在WebLogic下部署的Spring web服务,我不需要提供应用程序上下文。它们对我来说工作得很好-SOAP UI客户端在使用它们时没有问题。我认为你在做其他错误的事情。男孩,一个简单的例子来说明你的意思会非常有用。+1感谢你在directi中给我指点在SpringBeanAutowiringSupport的第二个版本中,我一直在努力让Glassfish放弃对JAX-WS的管理,让Spring参与其中。这是一个简单得多的解决方案,让Spring专注于它擅长的事情。我一直在尝试让解决方案发挥作用,但SpringBeanAutowiringSupport似乎没有o对我的web服务产生影响。
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;
  ...
}