Rod*_*yas 5 java interceptor cdi
我正在使用CDI拦截器,我意识到只有在使用@Interceptor注释的类中的第一个方法调用才会被截获.在下面的示例中,方法B从未被截获.
@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();
}
}
Run Code Online (Sandbox Code Playgroud)
如果首先调用methodA,我怎样才能截获该方法?有一些解决方法吗?
这是因为您methodB()直接调用而不是通过CDI代理调用,因此永远不会调用拦截器.只有在使用其代理调用CDI bean方法时才会调用拦截器.您应该将方法B移动到另一个CDI bean中并将@Inject其移动到此一个并从methodA更改methodB()为bean2.methodB(..).
| 归档时间: |
|
| 查看次数: |
2262 次 |
| 最近记录: |