JAX-WS 和 Guice 3

2024-04-28

是否有某种方法可以获取使用 JAX-WS 创建的 SOAP Web 服务类,并使用 Guice 3.0(guice-persist)事务或什至只是简单的依赖注入来注入它们? guiceyfruit 包提供了一个 @GuiceManaged 注释,使 Guice 2.0 成为可能,但 guiceyfruit(根据我的测试)似乎与 Guice 3 不兼容,我认为该项目不再活跃。

也许是因为还有另一种方法可以做到这一点?也许是 JSR 标准方式?


不久前我遇到了同样的问题,我查看了 Guicyfruit 代码并决定提取我需要的内容。这导致了三个班级。

首先,我们需要一个注释,可以用来注释我们的 Web 服务端点。

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。

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;
    }
}

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

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 进行日志记录,但当然您可以随意使用。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

JAX-WS 和 Guice 3 的相关文章

随机推荐