Module Google Guice、拦截器和私有模块

Module Google Guice、拦截器和私有模块,module,guice,private,Module,Guice,Private,这里有新的海报,希望我没有违反任何规则:) 我在GoogleGuice中使用PrivateModule,以便为同一环境提供多个数据源。但是我很难让MethodInterceptor在私有模块中工作。 下面是一个解释“问题”的简单测试用例 一个简单的服务类别是: interface Service { String go(); } class ServiceImpl implements Service { @Override @Transactional public

这里有新的海报,希望我没有违反任何规则:)

我在GoogleGuice中使用PrivateModule,以便为同一环境提供多个数据源。但是我很难让MethodInterceptor在私有模块中工作。 下面是一个解释“问题”的简单测试用例

一个简单的服务类别是:

interface Service {
    String go();
}

class ServiceImpl implements Service {

    @Override @Transactional
    public String go() {
        return "Test Case...";
    }

}
class MyModule extends AbstractModule {

    @Override
    protected void configure() {
        install(new PrivateModule() {
            @Override
            protected void configure() {
                bind(Service.class).to(ServiceImpl.class);
                bindInterceptor(
                    Matchers.any(),
                    Matchers.annotatedWith(Transactional.class),
                    new MethodInterceptor() {
                    @Override
                    public Object invoke(MethodInvocation i)
                            throws Throwable {
                        System.out.println("Intercepting: "
                                + i.getMethod().getName());
                        return i.proceed();
                    }
                });
                expose(Service.class);
            }
        });
    }

}
MyModule
类将是:

interface Service {
    String go();
}

class ServiceImpl implements Service {

    @Override @Transactional
    public String go() {
        return "Test Case...";
    }

}
class MyModule extends AbstractModule {

    @Override
    protected void configure() {
        install(new PrivateModule() {
            @Override
            protected void configure() {
                bind(Service.class).to(ServiceImpl.class);
                bindInterceptor(
                    Matchers.any(),
                    Matchers.annotatedWith(Transactional.class),
                    new MethodInterceptor() {
                    @Override
                    public Object invoke(MethodInvocation i)
                            throws Throwable {
                        System.out.println("Intercepting: "
                                + i.getMethod().getName());
                        return i.proceed();
                    }
                });
                expose(Service.class);
            }
        });
    }

}
最后一个测试用例:

public class TestCase {

    @Inject Service service;

    public TestCase() {
        Guice.createInjector(new MyModule()).injectMembers(this);
    }

    public String go() {
        return service.go();
    }

    public static void main(String[] args) {
        TestCase t = new TestCase();
        System.out.println(t.go());
    }

}
您希望输出为:

Intercepting: go
Test Case...
但是它没有发生,拦截器没有被使用,ant只
测试用例…
被输出

如果我绑定/公开
ServiceImpl
而不是接口,那么它就会工作

提前感谢,, 当做
嗯。。。我在贴出问题后不久就发现了:)

问题是您还需要
expose()
服务impl类。 因此绑定/暴露将是。

问候,

LL

您需要在专用模块中显式绑定ServiceImpl。现有代码的问题在于它从父模块继承了ServiceImpl的绑定。从文件来看

专用模块使用主喷油器实现。当它能够满足它们的依赖关系时,将在根环境中创建即时绑定。这种绑定在树中的所有环境中共享

添加此行应该可以解决以下问题:

bind(ServiceImpl.class);

不,这还不够,还需要公开绑定,正如我在上一篇文章中所说的。不!你只需要把它绑起来。从私有模块中公开它是不必要的(并且在某种程度上违背了私有模块的观点)。