Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the interceptor not called in the same service class?

I want to use interceptors in a Java-SE application and I am using weld as CDI implementation and i'm testing this here:

The Main-Class:

 public static void main(String[] args) {

    WeldContainer weldContainer = new Weld().initialize();

    Service service = weldContainer.instance().select(Service.class).get();
    service.methodCall();
    service.methodCallNumberTwo();

}

The Service-Class:

  public class Service {

    @TestAnnotation
    public void methodCall(){

      System.out.println("methodCall...!");
      methodCallNumberTwo();

    }

    @TestAnnotation
    public void methodCallNumberTwo(){

      System.out.println("methodCallNumberTwo...!");

    }

 }

The Interceptor-Class:

 @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 the output:

 I'm the TestInterceptor of public void Service.methodCall()
 methodCall...!
 methodCallNumberTwo...!
 I'm the TestInterceptor of public void Service.methodCallNumberTwo()
 methodCallNumberTwo...!

My Questions

First: Why isn't the interceptor called in methodCall() when i'm calling methodCallNumberTwo()?

Second: Is there a way to change that?

I'm only studying the behavior of interceptors and want to understand. Thank you in advance!

like image 645
Qri Avatar asked Jul 13 '13 08:07

Qri


People also ask

How do you call an interceptor in Java?

Use the @AroundInvoke annotation to designate interceptor methods for managed object methods. Only one around-invoke interceptor method per class is allowed. Around-invoke interceptor methods have the following form: @AroundInvoke visibility Object method-name(InvocationContext) throws Exception { ... }

How interceptors are declared?

An interceptor is defined using one of the interceptor metadata annotations listed in Table 50-1 within the target class, or in a separate interceptor class. The following code declares an @AroundTimeout interceptor method within a target class. @Stateless public class TimerBean { ...

Why do we use interceptor 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.

What is interceptor in common architecture in Java?

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.


1 Answers

The interceptor is not called because you are calling it on the same instance of the object. If you're familiar with EJBs it's the same as calling a method on the same object instead of through the EJB context.

If you debug through it you'll notic that the method call on the injected objects goes through a proxy. The method call from methodOne to methodTwo isn't proxied.

like image 149
LightGuard Avatar answered Nov 01 '22 08:11

LightGuard