Home > Mobile >  AroundInvoke: Curious name?
AroundInvoke: Curious name?

Time:11-10

Request more information on this interceptor annotation. Why is the interceptor annotation called @AroundInvoke and not @BeforeInvoke?

Can I for example invoke an access verification before an action API? What is the guarantee that the Access Verification is completed before the method is actually invoked? Is there something that the VM or the CDI implementation do that does not prevent the actual invocation but execute this interceptor in parallel?

If I were using the Google Guice AOP Method Interceptors, I am assured that the Access Verification failure will determine whether or not the method invocation BEGINS. How can I assure myself of a similar behavior from the jakarta CDI?

Have not been able to find this information in the spec

A related question can be found here. But the exact questions above remain unanswered.

CodePudding user response:

@AroundInvoke is called that because it can act both before and after the actually invoked method. Look at the documentation and its sample:

@AroundInvoke
public Object intercept(InvocationContext ctx) throws Exception { ... }

In that method you can call ctx.proceed() to call the target method (or any other interceptors). When you do stuff before that call then you act before the method and anything you do after that call happens after the method is called. Therefore it's "around" the method.

Sample:

@AroundInvoke
public Object intercept(InvocationContext ctx) throws Exception {
   log.info("We're about to do the thing!");
   Object result = ctx.proceed();
   log.info("We did the thing!");
   return result;
}
  • Related