與How can I access the ServletContext from within a JAX-WS web service?類似,有沒有辦法訪問applicationContext,比這更容易?如何從JAX-WS Web服務中訪問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;
}
}
那麼,我該如何與Spring服務協作,如果我不能說:appContext.getBean('myBean')? – pihentagy 2009-03-03 11:08:05
通過setter或構造函數注入它。依賴注入意味着「不要打電話給我們,我們會打電話給你。」你的對象不必擁有應用上下文來獲得他們需要的東西。 – duffymo 2009-03-03 13:47:13