与类似我可以从JAX-WS Web服务中访问ServletContext吗?,有没有办法访问applicationContext,比这更容易?
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; } }推荐答案
我不认为Web服务应该必须了解Web或servlet上下文或其应用程序上下文。我不明白为什么它应该知道这些。它不应该更被动吗?注入它需要的东西并让它完成它的工作。与客户的服务交互应基于预先定义的合同。如果它必须从某种类型的上下文中获取未知值,客户将如何知道需要设置什么或如何设置它?
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?
我会更进一步说一个Web服务应该是Spring服务接口的包装器。它是揭露它的所有可能方式中的又一个选择。您的Web服务应该只执行编组和解组XML请求/响应对象并与Spring服务协作。
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.
更多推荐
如何从JAX
发布评论