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

前端 未结 5 1383
暗喜
暗喜 2021-02-15 08:48

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?

import javax.ann         


        
相关标签:
5条回答
  • 2021-02-15 08:54

    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?

    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.

    0 讨论(0)
  • 2021-02-15 08:58

    Make your web service bean extend a spring bean.

    like this

    0 讨论(0)
  • 2021-02-15 09:09
    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;
      ...
    }
    
    0 讨论(0)
  • 2021-02-15 09:19

    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

    Read the NOTE: at the end of the javadoc.

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

    0 讨论(0)
  • 2021-02-15 09:20

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

    0 讨论(0)
提交回复
热议问题