Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java CDI. Interceptor is only invoked in the first method call in a class [duplicate]

I'm using CDI Interceptors and I've realized that only the first method call in a class annotated with @Interceptor is intercepted. In the example below methodB is never intercepted.

@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();
    }
}

How can I achieve that methodB be intercepted if methodA is called first? is there some workaround?

like image 564
Rodrigo Villalba Zayas Avatar asked Aug 20 '15 23:08

Rodrigo Villalba Zayas


People also ask

How does interceptor work in Java?

Interceptors are used in conjunction with Java EE managed classes to allow developers to invoke interceptor methods on an associated target class, in conjunction with method invocations or lifecycle events. Common uses of interceptors are logging, auditing, and profiling.

How do you intercept a method call in Java?

If we want a specific method to be a pointcut, we have to annotate it with the @AroundInvoke annotation. The @AroundInvoke method (or the pointcut method) must return an object, and must have a parameter of the InvocationContext type.

What is CDI interceptor?

An interceptor is a class used to interpose in method invocations or lifecycle events that occur in an associated target class. The interceptor performs tasks, such as logging or auditing, that are separate from the business logic of the application and are repeated often within an application.

What is the role of AroundInvoke method?

An AroundInvoke method can invoke any component or resource that the method it is intercepting can invoke. AroundInvoke method invocations occur within the same transaction and security context as the method on which they are interposing.


1 Answers

It's because you are calling methodB() directly and not via the CDI proxy so the interceptor is never invoked. Interceptors will only be invoked when the CDI bean method is called using its proxy. You should move method B into another CDI bean and @Inject it into this one and from methodA change methodB() to bean2.methodB(..).

like image 100
NBW Avatar answered Sep 28 '22 03:09

NBW