10

类似于如何从 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;
    }
}
4

5 回答 5

8
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;
  ...
}
于 2009-12-03T01:08:23.133 回答
1

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

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

于 2009-03-02T14:08:19.777 回答
1

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

这样

于 2009-08-20T14:23:10.943 回答
0

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

于 2009-03-02T13:31:06.857 回答
0

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

阅读注释:在 javadoc 的末尾。

实际上,最初的问题可能是应该实施的方式。

于 2014-09-03T16:42:53.680 回答