如何从JAX

编程入门 行业动态 更新时间:2024-10-24 22:22:43
本文介绍了如何从JAX-WS Web服务中访问ApplicationContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

与类似我可以从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

本文发布于:2023-11-08 14:16:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569606.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:JAX

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!