首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JAX-WS和Guice 3

JAX-WS和Guice 3
EN

Stack Overflow用户
提问于 2011-08-15 12:05:12
回答 2查看 1.9K关注 0票数 3

有没有什么方法可以把用JAX-WS创建的SOAP web服务类注入进来,比如说,Guice 3.0 (guice-persist)事务,或者仅仅是简单的依赖注入?guiceyfruit包提供了一个@GuiceManaged注解,使得这在Guice 2.0中成为可能,但Guice (根据我的测试)似乎与Guice 3不兼容,我认为该项目不再活跃。

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-11-11 01:13:16

前段时间我也遇到了同样的问题,我看了一眼瓜果的代码,决定提取我需要的东西。这导致了三个类。

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

GuiceManaged.java

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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进行日志记录,但当然要由您自己决定是否使用您喜欢的内容。

票数 1
EN

Stack Overflow用户

发布于 2011-08-15 20:46:06

在我的日志中,我发现了一些错误,这些错误与在3.0中不再存在的guicyfruit调用2.0内部机制有关。从GuiceManaged代码中,它实际上只进行了一次out调用,所以我决定去掉依赖项,并四处寻找替代方法。

http://code.google.com/p/guice-recipes/有一个叉子,它可能解决问题,也可能不解决问题,如果用它来代替瓜果的话。

我使用了http://code.google.com/p/google-guice/issues/detail?id=288#c69代码,并将其用作GuiceManagedInstanceResolver中的新dispose()方法。

现在,我的代码中有了可用的Guice注入和方面,并且没有与缺少方法和/或内存泄漏相关的日志输出。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7061626

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档