In depth understanding of the execution sequence of Spring and Aop

  • 2021-09-16 06:56:29
  • OfStack

First, recall the common annotations of AOP under 1

@ Before: Pre-notification: Executed before target method @ After: Post notification: Executed after target method @ AfterReturning: Notification after return: Execute before the end of the execution method @ AfterThrowing: Exception notification: Executed when an exception occurs @ Around: Wrap Notification: Wrap Target Method Execution

aop normal sequence + abnormal sequence in Spring4


try{
    @Before
    method.invoke(obj, args);
    @AfterReturning
}catch(){
    @AfterThrowing
}finally{
    @After
}

Normal execution: @ Before (pre-notification) = = = = > @ After (post notification) = = = = > @ AfterReturning (normal return)
Exception execution: @ Before (pre-notification) = = = = > @ After (post notification) = = = = > @ AfterThrowing (method exception)

In addition, if wrap notifications are also configured, the pre-processing of wrap notifications is performed before @ Before

Post-processing around notifications is also performed before @ After, @ AfterReturning, @ AfterThrowing

Normal results:


 Wrap preprocessing 
@Before Pre-notification 
invokeMethod()
 Post-processing of surround notification 
@After Post notification 
@AfterReturning / @AfterThrowing

Exception result:


 Wrap preprocessing 
@Before Pre-notification 
@After Post notification 
@AfterReturning / @AfterThrowing

aop normal sequence + abnormal sequence in Spring5

Normal execution: @ Before (pre-notification) = = = = > @ AfterReturning (normal return) = = = = > @ After (post notification)
Exception execution: @ Before (pre-notification) = = = = > @ AfterThrowing (method exception) ==== > @ After (post notification)

Add the normal execution results around the notification:


 Wrap preprocessing 
@Before Pre-notification 
invokeMethod()
@AfterReturning / @AfterThrowing
@After Post notification 
 Post-processing of surround notification 

Add the exception execution result around the notification:


 Wrap preprocessing 
@Before Pre-notification 
@AfterReturning / @AfterThrowing
@After
```## Spring Aop Execution order of 

First, recall the common annotations of ` AOP ` under 1

+ @ Before: Pre-notification: Executed before target method

+ @ After: Post notification: Executed after target method
+ @ AfterReturning: Notification after return: Execute before the execution method ends
+ @ AfterThrowing: Exception notification: Executed when an exception occurs
+ @ Around: Wrap Notification: Wrap Target Method Execution

> aop normal sequence + abnormal sequence in Spring4


try{
    @Before
    method.invoke(obj, args);
    @AfterReturning
}catch(){
    @AfterThrowing
}finally{
    @After
}

Normal execution: @ Before (pre-notification) = = = = > @ After (post notification) = = = = > @ AfterReturning (normal return)
Exception execution: @ Before (pre-notification) = = = = > @ After (post notification) = = = = > @ AfterThrowing (method exception)

In addition, if wrap notifications are also configured, the pre-processing of wrap notifications is performed before @ Before

Post-processing around notifications is also performed before @ After, @ AfterReturning, @ AfterThrowing

Normal results:


 Wrap preprocessing 
@Before Pre-notification 
invokeMethod()
 Post-processing of surround notification 
@After Post notification 
@AfterReturning / @AfterThrowing

Exception result:


 Wrap preprocessing 
@Before Pre-notification 
@After Post notification 
@AfterReturning / @AfterThrowing

aop normal sequence + abnormal sequence in Spring5

Normal execution: @ Before (pre-notification) = = = = > @ AfterReturning (normal return) = = = = > @ After (Post Notification)
Exception execution: @ Before (pre-notification) = = = = > @ AfterThrowing (method exception) ==== > @ After (Post Notification)

Add the normal execution results around the notification:


 Wrap preprocessing 
@Before Pre-notification 
invokeMethod()
@AfterReturning / @AfterThrowing
@After Post notification 
 Post-processing of surround notification 

Add the exception execution result around the notification:


 Wrap preprocessing 
@Before Pre-notification 
@AfterReturning / @AfterThrowing
@After



Related articles: