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
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.
Make your web service bean extend a spring bean.
like this
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;
...
}
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.
I would install a Filter that saves ServletContext before chaining in a ThreadLocal