在 Java 6 外部 Web 容器中使用 Guice 3 和 JAX-WS
我们的情况是,我们使用基于 JSR-330 的注入来配置独立的 Java 6 应用程序,这对于获取所有层的配置参数非常有效。
我们还使用 JAX-WS Web 服务相当长一段时间,通过在 Web 容器内使用第一个独立的 Metro 发行版和 Java 5,但在 Java 6 中,我们只使用 Endpoint 类来获得更小的占用空间。
所以现在我遇到的情况是,我有
- 一个独立的 Java 6 应用程序 - 没有 servlet 容器(Jetty、Tomcat),
- 并且按照我喜欢的方式设置了 Guice 3 注入器。
- 一个
Endpoint
处理我的@javax.jws.WebService
带注释的类,该类将我的方法公开为 Web 服务。
我希望 Web 服务方法要么透明地处理其 @Inject 字段,要么访问注入器。我可以从主方法中将其作为静态字段获取,但我想要一个更干净的解决方案。
有什么建议吗?
(我从 JAX-WS 和 Guice 3 了解到 http://jax-ws-commons.java.net/guice/ 模块不适用于Guice 3,建议的解决方法是 Tomcat 特定的)
JSR-250 @Resource
注释在这里有用吗?
We have a situation where we use JSR-330 based injections to configure our stand-alone Java 6 applications, which works very well for getting configuration parameters across all the layers.
We have also used JAX-WS web services for quite a while by using first stand-alone Metro distribution with Java 5 inside a web container, but with Java 6 we just use the Endpoint class to get a smaller footprint.
So now I have a situation where I have
- A stand-alone Java 6 application - no servlet container (Jetty, Tomcat)
- A Guice 3 Injector set up as I like it.
- An
Endpoint
handling my@javax.jws.WebService
annotated class which expose my methods as web services.
I would like the web service methods to either have their @Inject fields handled transparently, or to get access to the injector. I can grab it as a static field from the main method, but I'd like a cleaner solution.
Any suggestions?
(I understand from JAX-WS and Guice 3 that the http://jax-ws-commons.java.net/guice/ module does not work with Guice 3, and the workaround suggested is Tomcat specific)
Would JSR-250 @Resource
annotations be useful here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定我是否理解了这个问题的每一点。 +500 赏金看起来太容易了。如果这不是您要搜索的内容,请发布一些代码。
不管怎样,一个简单的解决方案创建了一个带有依赖注入的网络服务:
下面是一个带有类路径扫描的更复杂的解决方案(Reflections )基于来自 JAX-WS Guice 集成 的 Marcus Eriksson 代码。它将使用
@GuiceManaged
注解的所有类发布为Endpoint.publish()
的 Web 服务。GuiceManaged
注释:以及
HelloServiceImpl
片段:I'm not sure that I've understood every bit of the question. It looks to too easy for +500 bounty. Please post some code if that's not what you're searching for.
Anyway, a simple solution which creates a web service with dependency injection:
Below a more sophisticated solution with classpath scanning (Reflections) based on Marcus Eriksson's code from JAX-WS Guice integration. It publishes all classes which is annotated with
@GuiceManaged
as a webservice withEndpoint.publish()
.The
GuiceManaged
annotation:And the
HelloServiceImpl
snippet:您需要使用
AbstractMultiInstanceResolver
扩展点。创建注释
GuiceManaged
;实现
GuiceManagedFeature
,即WebServiceFeature
:通过扩展
AbstractMultiInstanceResolver
实现InstanceResolver
现在用
@GuiceManaged 注释您的服务
&在您的业务方法上使用@Inject
进行方法级 DI。you need to use the
AbstractMultiInstanceResolver
extension point.create the annotation
GuiceManaged
;implement the
GuiceManagedFeature
which isWebServiceFeature
:Implement
InstanceResolver
by ExtendingAbstractMultiInstanceResolver
Now Annotate your Service with
@GuiceManaged
& use@Inject
for method level DI on your business method.