One, five notification types:
Notice: When to execute the aspect method
Daily development is not commonly used, but interviews and written examinations will often be asked
-
After Returning Advice and After Throwing Advice are mutually exclusive
-
Before Advice and After Advice: The effect is similar to the finally block in try–catch–finally, it will be executed anyway
-
To understand the first 4 types, just know the timing of triggering. Around Advice should focus on mastering.
Introduction enhancement: just understand
Two, give an example:
1. After Advice:
Disadvantages: unable to obtain the return value generated during the operation of the target method or the exception thrown internally
use:
- Definition method:
- Placement xml
- Call (omitted)
2. After returning Advice
-
The biggest difference with post-notification: you can accept the return value of the target method
-
The execution order of post-notification and post-return notification is determined by the writing order in xml, and there is no default execution order
-
Example
- Definition method: There are 2 return values
- Placement xml
- Call (omitted)
3. Exception notification (After Throwing Advice)
- Same as above, the order of log generation is in the order of configuration
-
Definition method: (with 2 parameters)
-
xml file for configuration
-
Trigger exception notification
-
Run (omitted)
4. Surround notification (emphasis!!!)
-
The most powerful notification, recommended
-
Note: The method of surrounding notification needs to add the return value Object (the return value is the return value of the method that triggered the surrounding notification)
-
The proceed() method -> ProceedingJoinPoin parameter calls this method, the target method will be executed, otherwise the target method will never be executed (the core method of controlling the target method), (the return value is the return value of the target method)
-
The reason for handling the exception here:
Usually, multiple notifications may be laid out in a program. If we catch and handle the exception here, the subsequent notification program cannot catch this exception, and unexpected problems may occur.
- Configuration method
- Configuration xml file