在Web容器外的Java 6中使用Guice 3和JAX-WS [英] Using Guice 3 with JAX-WS in Java 6 outside web container
问题描述
我们有一种情况,我们使用基于JSR-330的注入来配置我们的独立Java 6应用程序,这非常适合在所有层中获取配置参数。
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.
我们还使用JAX-WS Web服务已经有一段时间了,在Web容器中使用Java 5的第一个独立Metro分发,但是使用Java 6我们只使用Endpoint类来减少占用空间。
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
- 一个独立的Java 6应用程序 - 没有servlet容器(Jetty,Tomcat)
- 一个Guice 3 Injector设置我喜欢它。
- 一个
端点
处理我的@javax.jws.WebService
带注释的类,它将我的方法公开为Web服务。
- 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.
我希望Web服务方法能够透明地处理@Inject字段,或者访问注入器。我可以从主方法中将其作为静态字段抓取,但我想要一个更清洁的解决方案。
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.
有什么建议吗?
(我从 JAX-WS了解到这一点和Guice 3 即 http://jax-ws-commons.java。 net / guice / 模块不适用于Guice 3,建议的解决方法是特定于Tomcat)
(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)
JSR-250 @资源
注释在这里有用吗?
Would JSR-250 @Resource
annotations be useful here?
推荐答案
我不确定我是否理解过每一点这个问题。它看起来太容易了+500赏金。如果这不是您要搜索的内容,请发布一些代码。
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.
无论如何,这是一个创建带依赖注入的Web服务的简单解决方案:
Anyway, a simple solution which creates a web service with dependency injection:
final Module module = new HelloModule();
final Injector injector = Guice.createInjector(module);
final HelloService helloService = injector.getInstance(HelloService.class);
Endpoint.publish("http://localhost:8080/helloService", helloService);
下面是一个更复杂的类路径扫描解决方案(思考)基于Marcus Eriksson的代码 JAX-WS Guice集成。它使用 Endpoint.publish()
发布所有使用 @GuiceManaged
注释的类作为Web服务。
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 with Endpoint.publish()
.
private void initGuicedWebservices(final String packageNamePrefix)
throws Exception {
final Reflections reflections = new Reflections(packageNamePrefix);
final Set<Class<?>> guiceManaged =
reflections.getTypesAnnotatedWith(GuiceManaged.class);
for (final Class<?> clazz : guiceManaged) {
doGuice(clazz);
}
}
private void doGuice(final Class<?> clazz) throws Exception {
final GuiceManaged guiceManagedAnnotation =
clazz.getAnnotation(GuiceManaged.class);
final Injector injector = createInjector(guiceManagedAnnotation);
final Object serviceObject = clazz.newInstance();
injector.injectMembers(serviceObject);
final String address = guiceManagedAnnotation.address();
Endpoint.publish(address, serviceObject);
}
private Injector createInjector(final GuiceManaged guiceManagedAnnotation)
throws Exception {
final Class<? extends Module>[] moduleClasses =
guiceManagedAnnotation.module();
final List<Module> moduleInstances = new ArrayList<Module>();
for (final Class<? extends Module> moduleClass : moduleClasses) {
moduleInstances.add(moduleClass.newInstance());
}
return Guice.createInjector(moduleInstances);
}
GuiceManaged
注释:
@Retention(RUNTIME)
@Target(TYPE)
@Documented
public @interface GuiceManaged {
public Class<? extends Module>[] module();
public String address();
}
HelloServiceImpl
snippet:
@GuiceManaged(module = HelloModule.class,
address = "http://localhost:8080/helloService")
@WebService
public class HelloServiceImpl implements HelloService {
@Inject // bound in HelloModule
public GreetingsService greetingsService;
@Override
@WebMethod
public String sayHello(final String name) {
return greetingsService.sayHello(name);
}
}
这篇关于在Web容器外的Java 6中使用Guice 3和JAX-WS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!