JAX-WS和Guice 3 [英] JAX-WS and Guice 3

查看:75
本文介绍了JAX-WS和Guice 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有某种方法可以使用JAX-WS创建的SOAP Web服务类注入Guice 3.0(guice-persist)事务,甚至仅仅是普通的依赖注入? guiceyfruit软件包提供了一个@GuiceManaged批注,这使得Guice 2.0可以做到这一点,但是guiceyfruit(根据我的测试)似乎与Guice 3不兼容,我认为该项目不再有效.

Is there some way to take SOAP web service classes creates with JAX-WS and inject them with, say, Guice 3.0 (guice-persist) transactions or even just plain ol' dependency injection? The guiceyfruit package provided a @GuiceManaged annotation that made this possible with Guice 2.0, but guiceyfruit (from my testing) appears to be incompatible with Guice 3, and I don't think the project is active any longer.

也许是因为还有另一种方法吗?也许是JSR标准方式?

Perhaps because there is another way of doing this? Maybe a JSR standard way?

推荐答案

前一段时间我遇到了同样的问题,我看了一下Guicyfruit代码并决定提取所需的内容.这导致了三堂课.

I came across this same issue a while back and I had a look at the Guicyfruit code and decided to extract what I needed. This resulted in three classes.

首先,我们需要一个注释,我们可以使用该注释来注释我们的Web服务终结点.

First we need an annotation that we can use to annotate our web service endpoint with.

GuiceManaged.java

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.xml.ws.spi.WebServiceFeatureAnnotation;
import com.google.inject.Module;
import com.sun.xml.ws.api.server.InstanceResolverAnnotation;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@WebServiceFeatureAnnotation(id = GuiceManagedFeature.ID, bean = GuiceManagedFeature.class)
@InstanceResolverAnnotation(GuiceManagedInstanceResolver.class)
public @interface GuiceManaged {
    Class<? extends Module>[] modules();
}

第二,我们需要上面注释中提到的GuiceManagedFeature.

Second we need the GuiceManagedFeature mentioned in the annotation above.

GuiceManagedFeature.java

import javax.xml.ws.WebServiceFeature;
import com.sun.xml.ws.api.FeatureConstructor;

public class GuiceManagedFeature extends WebServiceFeature {

    public static final String ID = "any.string.will.do.here";

    @FeatureConstructor
    public GuiceManagedFeature() {
        this.enabled = true;
    }

    @Override
    public String getID() {
        return ID;
    }
}

第三,我们创建实际的解析器.

And third we create the actual resolver.

GuiceManagedInstanceResolver.java

import java.util.ArrayList;
import java.util.List;
import javax.xml.ws.WebServiceContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.sun.xml.ws.api.message.Packet;
import com.sun.xml.ws.api.server.WSEndpoint;
import com.sun.xml.ws.api.server.WSWebServiceContext;
import com.sun.xml.ws.server.AbstractMultiInstanceResolver;

public class GuiceManagedInstanceResolver<T> extends AbstractMultiInstanceResolver<T> {

    private static final Logger LOGGER = LoggerFactory.getLogger(GuiceManagedInstanceResolver.class);

    private static Injector injector;

    private transient WSWebServiceContext webServiceContext;

    public GuiceManagedInstanceResolver(final Class<T> clazz) {
        super(clazz);
    }

    @Override
    public T resolve(final Packet request) {
        final T instance = injector.getInstance(this.clazz);
        injector.injectMembers(instance);

        return instance;
    }

    @SuppressWarnings("rawtypes")
    @Override
    public void start(final WSWebServiceContext wsWebServiceContext, final WSEndpoint endpoint) {
        super.start(wsWebServiceContext, endpoint);
        this.webServiceContext = wsWebServiceContext;

        synchronized (GuiceManagedInstanceResolver.class) {
            if (injector == null) {
                final List<Module> moduleInstances = new ArrayList<Module>();
                final Class<? extends Module>[] modules = this.clazz.getAnnotation(GuiceManaged.class).modules();

                for (final Class<? extends Module> moduleClass : modules) {
                    try {
                        moduleInstances.add(moduleClass.newInstance());
                    } catch (final InstantiationException exception) {
                        LOGGER.error("Could not instantiate guice module [{}]", moduleClass.getName());
                    } catch (final IllegalAccessException e) {
                        LOGGER.error("Could not instantiate guice module [{}]", moduleClass.getName());
                    }
                }

                moduleInstances.add(new AbstractModule() {
                    @Override
                    protected void configure() {
                        this.bind(WebServiceContext.class).toInstance(GuiceManagedInstanceResolver.this.webServiceContext);
                    }
                });

                injector = Guice.createInjector(moduleInstances);
            }
        }
    }
}

上面的示例使用SLF4J进行日志记录,但是当然可以根据需要使用任何东西.

The above example uses SLF4J for logging but it's of course up to you to use whatever you wish.

这篇关于JAX-WS和Guice 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆