3
我想在一個Java SE應用程序中使用攔截器,我使用焊接作爲CDI實現和我在這裏測試此:爲什麼攔截器不在相同的服務類中調用?
主類:
public static void main(String[] args) {
WeldContainer weldContainer = new Weld().initialize();
Service service = weldContainer.instance().select(Service.class).get();
service.methodCall();
service.methodCallNumberTwo();
}
的服務-Class:
public class Service {
@TestAnnotation
public void methodCall(){
System.out.println("methodCall...!");
methodCallNumberTwo();
}
@TestAnnotation
public void methodCallNumberTwo(){
System.out.println("methodCallNumberTwo...!");
}
}
攔截器級:
@Interceptor
@TestAnnotation
public class TestInterceptor {
@AroundInvoke
public Object interceptorMethod(InvocationContext invocationContext) throws Exception {
System.out.println("I'm the TestInterceptor of "+invocationContext.getMethod());
return invocationContext.proceed();
}
}
Aaaand輸出:
I'm the TestInterceptor of public void Service.methodCall()
methodCall...!
methodCallNumberTwo...!
I'm the TestInterceptor of public void Service.methodCallNumberTwo()
methodCallNumberTwo...!
我的問題
第一:爲什麼沒有攔截稱爲methodCall()當我打電話methodCallNumberTwo()?
第二:有沒有辦法改變這種情況?
我只是學習攔截器的行爲,並希望瞭解。先謝謝你!
可能有[Java CDI。攔截器僅在類中的第一個方法調用中調用](http://stackoverflow.com/questions/32129910/java-cdi-interceptor-is-only-invoked-in-the-first-method-call-in-一類) – lucasvc