這裏新的海報,希望我沒有剎車任何規則:)谷歌吉斯,攔截器和PrivateModules
我在谷歌 - 吉斯使用PrivateModule以擁有多個數據源對相同的環境。但是我很難讓MethodInterceptor在私有模塊中工作。 下面是一個解釋「問題」的簡單測試用例。
一個簡單的服務類將是:
interface Service {
String go();
}
class ServiceImpl implements Service {
@Override @Transactional
public String go() {
return "Test Case...";
}
}
的MyModule
類將是:
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...
但是沒有發生,攔截器未被使用,螞蟻只輸出了Test Case...
。
如果我綁定/暴露ServiceImpl
而不是接口,那麼它的工作。
由於提前, 問候, LL
沒有that't不夠的,還需要公開的結合,正如我在過去的文章中說。 – luislobo 2012-03-05 23:42:32
不!你只需要綁定它。將其從私有模塊中公開是不必要的(並且有些可能會破壞私有模塊的特徵。)測試它以便自己查看。 – 2012-03-06 02:33:33