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 コンテキスト、サーブレット コンテキスト、またはそのアプリケーション コンテキストについて知る必要はないと思います。なぜそれを知る必要があるのか​​ わかりません。もっと受動的であるべきではないでしょうか。必要なものを注入し、機能させます。クライアントとのサービスの対話は、事前に定義された契約に基づく必要があります。何らかのコンテキストから不明な値を取得する必要がある場合、クライアントは何を設定する必要があるか、またはどのように設定する必要があるかをどのように知るのでしょうか?

さらに言えば、Web サービスは Spring サービス インターフェイスのラッパーであるべきだと言えます。これは、それを公開するために考えられるすべての方法の中から、もう 1 つの選択肢にすぎません。Web サービスは、XML 要求/応答オブジェクトをマーシャリングおよびアンマーシャリングし、Spring サービスと連携するだけのことを行う必要があります。

于 2009-03-02T14:08:19.777 に答える
0

ThreadLocal でチェーンする前に、ServletContext を保存する Filter をインストールします。

于 2009-03-02T13:31:06.857 に答える