【发布时间】:2015-10-15 10:02:14
【问题描述】:
我需要访问通过 Guice @Provides 提供的实例,所以我正在测试一种通过拦截器访问 @Provides 方法的方法......但没办法,拦截器永远不会分发。 我也不能改变 Provider Method Signature ,因为继承自另一个类....
public class MyModule extends AbstractModule{
public static class MyInterceptor implements MethodInterceptor{
@Override
public Object invoke(final MethodInvocation methodInvocation) throws Throwable {
System.out.println("MyModule ::: Intercepted@invoke! : "+ methodInvocation.getMethod().getName());
return methodInvocation.proceed();
}
}
@Provides
StampInterface getStamp(){
StampExampleImpl se = new StampExampleImpl();
se.setId("theID");
se.setTst(System.currentTimeMillis());
return se;
}
@Override
protected void configure() {
bindInterceptor(Matchers.any(),
Matchers.annotatedWith(Provides.class),
new MyInterceptor());
}
public static void main(String... args) {
StampInterface s = Guice.createInjector(new MyModule()).getInstance(StampInterface.class);
System.out.println( s.getTst());
System.out.println("---------------------------");
}
}
【问题讨论】:
标签: java guice interceptor