我正在使用JBoss 7.1和Java 1.6。向JAX-WS注入Guice POJO
我想通过JAX-WS端点集成Guice服务。使用Gunnar.Morling描述的拦截器模式,我可以在使用无状态bean作为web服务时正确地实例化Guice模块。但是我不能用一个简单的POJO注释的web服务来做同样的事情。这有可能让任何人找到解决方法。以下是我迄今为止的工作总结。
@UsesGuice @Interceptor
public class GuiceInterceptor {
@Inject
private GuiceInjectorHolderBean injectorHolder;
@AroundInvoke
public Object aroundAdvice(final InvocationContext ctx) throws Exception {
if (ctx.getTarget().getClass().isAnnotationPresent(UsesGuice.class)) {
final Injector injector = injectorHolder.getInjector();
injector.injectMembers(ctx.getTarget());
}
return ctx.proceed();
}
}
GuiceInjectorHolderBean是负责触发guice布线的sinlgeton bean。需要注解类遵循
@Retention(RUNTIME)
@Target(TYPE)
@InterceptorBinding
public @interface UsesGuice {}
的JAX-WS POJO类
@UsesGuice
@WebService(serviceName = "EchoServiceService", portName = "EchoServicePort", ame = "EchoServiceImpl", targetNamespace = "lala")
public class EchoServiceImpl implements EchoService
{
@Inject
MyGuiceInjection injection;
@Override
@WebMethod
public String sayHello(final String msg)
{
return "Hello " + injection.call(msg);
}
}
在此先感谢 迪米特里