我正在使用CDI攔截器,並且我意識到只有用@Interceptor註釋的類中的第一個方法調用纔會被攔截。在下面的例子中,methodB永遠不會被攔截。Java CDI。攔截器僅在類中的第一個方法調用中調用
@InterceptorBinding
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Transactional {
}
@Transactional
@Interceptor
public class TransactionsInterceptor {
@AroundInvoke
public Object transactionInterceptor(InvocationContext context) throws Exception {
System.out.println("Method="+context.getMethod().getName());
return context.proceed();
}
}
public Interface AnImportantInterface {
public void methodA();
public void methodB();
}
@Transactional
@ThreadScoped
public class AnImportantClass implements AnImportantInterface {
public void methodA() {
methodB();
}
public void methodB() {
//This method is never intercepted
}
}
public class AnotherImportantClass {
@Inject AnImportantInterface aui;
public void someMethod() {
aui.methodA();
}
}
如何首先調用methodA來實現該方法B被截獲?有一些解決方法嗎?
大部分是正確的,雖然它的值得指出的是自我注入不是在CDI規範中規定的功能。 –
@JohnAment正如你所指出的那樣,在當前的CDI規範中沒有規定自我注入,儘管這不是我在回答中得到的結果,如果它受到支持(就像它在EJB規範中那樣),它將爲更清晰的解決方案提供支持。對於那些感興趣的RFE在這裏:https://issues.jboss.org/browse/CDI-414 – NBW