This is the 23rd day of my participation in the August More Text Challenge.More challenges in August
What is the AOP
Aspect Oriented Programming (Aspect Oriented Programming
To put it more graphically, a program using AOP is like a burger: the AOP code is the outermost two slices of bread, and the program is the filling in the center of the burger.
However, our AOP program can choose to wrap above, below, or both
How do YOU define an AOP
Define a standard class and tag it with the following two annotations:
@aspect, which indicates that this is a standard Spring Aspect class
@Component, which means this needs to be handed over to Spring for lifecycle management
Execution order of multiple AOP
If the Order of execution is required, a third annotation @order needs to be added to the corresponding aspect class
Code examples:
@Aspect
@Component
@Order(1)
public class HttpResultAspect {
Copy the code
Where, the smaller the value of order, the earlier the order of the section execution, because of the characteristics of the section, there will be the following diagram of the execution of multiple sections
AOP interceptor expression
Regular writing
execution(* *.*(..) )
Examples of
execution (* com.rlzz.r9.mapper.*Mapper.findAll(..) )
The first '*', any type of return value the second '*', any package path (the two points in the package path denote the current package and its subpackages) the third '*', any method name in parentheses, any argumentsCopy the code
Example 2
For example, we need to write the expression to intercept com. Wb. UserService. The.findall (), here are some feasible expression
execution(* com.. * (..) ) execution(* com.wb.*(..) ) execution(* com.wb.*Service.*(..) ) execution(* com.wb.*Service.find*(..) ) execution(* com.wb.*Service.findAll(..) ) execution(* com.wb.. find*(..) )Copy the code
Complete sample code applied to methods
@Around("execution (* com.rlzz.r9.mapper.*Mapper.update(..) )" + " || execution (* com.rlzz.r9.mapper.*Mapper.save(..) )"
public <D extends RcsDto> D afterSaveOrUpdate(ProceedingJoinPoint joinPoint) {}Copy the code
Use multiple expressions
Can use logic operator or – > | |, and – > &&, multiple expression string together determine whether need to intercept the target method
@PointCut
Expressions are written directly to methods, and when we need to reuse expressions, we can pull them out using @pointcut
The sample code above can be modified to
@PointCut("execution (* com.rlzz.r9.mapper.*Mapper.update(..) )"
private void mapperUpdate(a){}
@PointCut("execution (* com.rlzz.r9.mapper.*Mapper.save(..) )"
private void mapperSave(a){}
@Around("mapperUpdate() || mapperSave()")
public <D extends RcsDto> D afterSaveOrUpdate(ProceedingJoinPoint joinPoint) {}Copy the code
AOP execution timing
More formally, there are five types of advice in AOP
@Before
Pre-notification, executed before intercepting methods
@Before("execution (* com.rlzz.r9.mapper.*Mapper.delete(..) )"
public void delete(org.aspectj.lang.JoinPoint joinPoint){}
Copy the code
@Around
Circling the notification, executed before intercepting the method, then requires us to manually execute the target method and manually return the value of the target method after execution
@Around("execution (* com.rlzz.r9.mapper.*Mapper.save(..) )"
public Object aroundFindAll(org.aspectj.lang.ProceedingJoinPoint joinPoint){
Object obj = joinPoint.proceed(); // This represents executing the body of the method in the interception method
return obj; // You need to manually return the result of the method execution (you can control the return result)
}
Copy the code
@After
Post notification, executed after intercepting methods
@After("execution (* com.rlzz.r9.mapper.*Mapper.update(..) )"
public void update(org.aspectj.lang.JoinPoint joinPoint){}
Copy the code
@AfterReturning
This is an enhanced annotation to @after to get the return value of the method after it executes, so we can do some checking on the return value
If the return value is of a reference type, we can even modify the value corresponding to the reference
@PointCut("execution (* com.rlzz.r9.mapper.*Mapper.findAll(..) )"
private void mapperFindAll(a){}
@AfterReturning(returning = "list", pointcut = "mapperFindAll()")
public void findAll(org.aspectj.lang.JoinPoint joinPoint, Object list){
// The name of the Object parameter, which must be consistent with the RETURNING value in the annotations
}
Copy the code
@AfterThrowing
Another enhanced annotation for @After is that if an exception is thrown after the target method executes, it will be intercepted by AOP, but this handling is different from catch catching in that AOP intercepts and handles the exception but still propagates to the caller at the next level
Single slice notification execution order
Multiple facets notify execution order
AOP intercepts attention points
class Study {
public void static f1(a){} / / (1)
public void f2(a){} / / (2)
public void f3(a){ / / (3)f2(); }}Copy the code
(1) Do not intercept static methods
(2) External call, can intercept
(3) When an external call is made, f3() can be intercepted, but f2() called in F3 () will not be intercepted simultaneously.