Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AspectJ "around" and "proceed" with "before / after"

Tags:

java

aop

aspectj

Let's say you have three advices: around, before and after.

1) Are before/after called when proceed is called in the around advice, or are they called before/after the around advice as a whole?

2) If my around advice does not call proceed, will the before/after advice be run anyway?

like image 883
Gérald Croës Avatar asked Aug 02 '13 12:08

Gérald Croës


People also ask

What is the use of AspectJ?

AspectJ Is Powerful, Mature, and Used in a Lot of Frameworks AspectJ is very mature, powerful and widely used in today's enterprise Java frameworks like Spring. Spring uses AspectJ to make it easy for the developers to use Transaction Management, or applying security (using Spring Security) in your application.

What does JoinPoint proceed do?

In summary, joinPoint. proceed(); means that you are calling the set method, or invoking it.

What is JoinPoint AspectJ?

JoinPoint is an AspectJ interface that provides reflective access to the state available at a given join point, like method parameters, return value, or thrown exception. It also provides all static information about the method itself.

What is pointcut and JoinPoint in spring?

JoinPoint: Joinpoint are points in your program execution where flow of execution got changed like Exception catching, Calling other method. PointCut: PointCut are basically those Joinpoints where you can put your advice(or call aspect). So basically PointCuts are the subset of JoinPoints.


1 Answers

With this Test

@Aspect public class TestAspect {     private static boolean runAround = true;      public static void main(String[] args) {         new TestAspect().hello();         runAround = false;         new TestAspect().hello();     }      public void hello() {         System.err.println("in hello");     }      @After("execution(void aspects.TestAspect.hello())")     public void afterHello(JoinPoint joinPoint) {         System.err.println("after " + joinPoint);     }      @Around("execution(void aspects.TestAspect.hello())")     public void aroundHello(ProceedingJoinPoint joinPoint) throws Throwable {         System.err.println("in around before " + joinPoint);         if (runAround) {             joinPoint.proceed();         }         System.err.println("in around after " + joinPoint);     }      @Before("execution(void aspects.TestAspect.hello())")     public void beforeHello(JoinPoint joinPoint) {         System.err.println("before " + joinPoint);     } } 

i have following output

  1. in around before execution(void aspects.TestAspect.hello())
  2. before execution(void aspects.TestAspect.hello())
  3. in hello
  4. after execution(void aspects.TestAspect.hello())
  5. in around after execution(void aspects.TestAspect.hello())
  6. in around before execution(void aspects.TestAspect.hello())
  7. in around after execution(void aspects.TestAspect.hello())

so you can see before/after are not called when proceed is called from within @Around annotation.

like image 192
Frank M. Avatar answered Sep 28 '22 23:09

Frank M.