Several common notification types in AOP and their basic functions:
@Before: Pre-advice, executed before the target method is executed.
@After: After notification, regardless of the method execution result (including exceptions), it will be executed after the target method is executed.
@AfterReturning: After-return notification, executed after the target method ends normally, but not executed if the method throws an exception.
@AfterThrowing: Exception notification, executed when the target method throws an exception.
@Around: Surround notification, which can perform custom operations before and after the target method is executed, and even decide whether to continue executing the target method
Differences in AOP execution order between Spring 4 and Spring 5
AOP execution order in Spring4
Normal execution order:
@Before (Before notification)
Target method execution
@After (after notification)
@AfterReturning
Exception execution order:
@Before (Before notification)
Target method execution (throws exception)
@After (after notification)
@AfterThrowing (Exception Notification)
If an around advice (@Around) is configured, the pre-processing of the around advice will be executed before @Before, and the post-processing of the around advice will also be executed before @After, @AfterReturning, and @AfterThrowing.
AOP execution order in Spring5
In Spring 5, the execution order of AOP has changed, mainly in the execution order of @AfterReturning and @After:
Normal execution order:
@Before (Before notification)
Target method execution
@AfterReturning
@After (after notification)
Exception execution order:
@Before (Before notification)
Target method execution (throws exception)
@AfterThrowing (Exception Notification)
@After (after notification)
If an around advice (@Around) is configured, the execution order of the around advice is still to execute its pre-processing first, then the target method execution, followed by other advices (@AfterReturning, @AfterThrowing, @After), and finally the post-processing of the around advice.