10

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.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;
    }
}
Community
  • 1
  • 1
pihentagy
  • 5,975
  • 9
  • 39
  • 58

5 Answers5

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;
  ...
}
maximdim
  • 8,041
  • 3
  • 33
  • 48
  • 2
    +1 Thanks for pointing me in the direction of `SpringBeanAutowiringSupport`. I had been struggling with getting Glassfish to relinquish its management of JAX-WS and letting Spring to it. This is a much easier solution and lets Spring stay focused on the things that it is good at. – Matthew T. Staebler Apr 21 '10 at 20:26
  • I've been trying to get the solution to work but SpringBeanAutowiringSupport just doesn't seem to have an effect on my web service. http://stackoverflow.com/questions/12869014/web-service-exposed-by-extending-springbeanautowiringsupport-is-failing-to-injec – AR3Y35 Oct 18 '12 at 18:49
1

Make your web service bean extend a spring bean.

like this

Community
  • 1
  • 1
Michael Wiles
  • 20,902
  • 18
  • 71
  • 101
1

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.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Well, then how can I collaborate with Spring services, if I cannot say: appContext.getBean('myBean')? – pihentagy Mar 03 '09 at 11:08
  • Inject it in via setter or constructor. Dependency injection means "don't call us; we'll call you." Your objects don't have to have the app context to get what they need. – duffymo Mar 03 '09 at 13:47
  • You cannot. If I test my web service under glassfish, a new webservice is created, and it is not configured :-o That was a one-day-long debugging to get this knowledge :( – pihentagy Mar 03 '09 at 17:09
  • I'm writing Spring web services that I'm deploying under WebLogic, and I don't have to supply the application context. They're working fine for me - SOAP UI clients have no problems working with them. I think you're doing something else wrong. – duffymo Mar 03 '09 at 18:03
  • So, you autowired the required beans in your Service class? – pihentagy Mar 17 '09 at 16:18
  • 1
    Boy, a simple example of what you mean would be really helpful. – dbreaux Apr 08 '14 at 21:14
0

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.

user1955401
  • 91
  • 1
  • 2
  • 8
0

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

Maurice Perry
  • 32,610
  • 9
  • 70
  • 97