AOP Introduction
1. Six notification methods
- Surround notification: Implement enhancements before and after the target method is executed, and can be used for logging, transaction processing, etc.
- Pre-Notice: Implement enhancements before the target method is executed, and can be used for permission control, etc.
- Post-return notification: Implement enhancements after the target method is successfully executed, which can be used to close streams, delete temporary files, etc.
- Post-notification: Implement enhancement after the target method is executed (execute regardless of whether the method has an exception), which can be used to free resources
- Exception notification: Implement enhancements when the target method throws an exception, which can be used to handle exceptions, log logs, etc.
- Introduce notification: Add some methods and attributes to the target class to modify the target class
Related Notes
-
@Aspect
: used to define a section, annotated on the section class -
@Pointcut
: Used to define point-cut expressions. When using them, you need to define a point-cut method. The return value of this method is void and the body of the method is empty. -
@Before
: Used to define pre-notifications, usually specifying a value attribute, the value can be an existing entry point or a point-cut expression can be defined -
@AfterReturning
: Used to define a post-return notification, usually specifying a value attribute, the value can be an existing entry point or a point-cut expression can be defined. -
@Around
: Used to define a surround notification, usually specifying a value attribute, the value can be an existing entry point or a point-cut expression can be defined -
AfterThrowing
: Used to define exception notifications, usually specifying a value attribute, the value can be an existing entry point or a point-cut expression can be defined. There is also a throwing attribute used to access the exception thrown by the target method. The value of this attribute is consistent with the formal parameter of the same name in the exception notification method. -
After
: Used to define a post-final notification, usually specifying a value attribute, the value can be an existing entry point or a point-cut expression can be defined
Two ways to define entry points
1. Notes
//Definition Note@Target() @Retention() @Documented public @interface SafetyHarness { String defaultValue() default ""; } //Initial Point@Pointcut("@annotation()") public void efficacyParameters(){ }
2. Expressions
//All methods of this controller can be specified directly/* execution():Expression body The first * represents the return type, and uses * to represent all types. There is a space between the * and the package name. The second * represents the class name The third * represents the method name ..Denotes any parameter */ @Pointcut("value = "execution(* .*.* (..)))") public void efficacyParameters(){ }
Two commonly used notifications to obtain parameters
1.@Before
@Before("efficacyParameters()") public void beforeEfficacyParameters(JoinPoint joinPoint){ //Get request parameters ServletRequestAttributes attributes = (ServletRequestAttributes) (); HttpServletRequest request = (attributes).getRequest(); Map<String, String[]> parameterMap = (); //Get through parameter name Object ext1 = ("ext1"); Object ext2 = ("ext2"); }
2.@Around
@Around("efficacyParameters()") public T beforeEfficacyParameters(ProceedingJoinPoint joinPoint){ //The interface returns the result, the result can be executed before or after the service Object proceed = (); Object[] obj = (); if (obj == null || == 0) { return T; } //Get request parameters Map<String, Object> fieldsName = getFieldsName(joinPoint); Object ext1 = ("ext1"); //POST request for forced transfer SafetyAuditPageVo ext2 = (SafetyAuditPageVo) ("ext"); //Modify the request parameters SafetyAuditPageVo vo = (SafetyAuditPageVo) obj[0]; ("XXXXXX"); obj[0] = vo; return (obj); } private static Map<String, Object> getFieldsName(ProceedingJoinPoint joinPoint) { Object[] args = (); ParameterNameDiscoverer pnd = new DefaultParameterNameDiscoverer(); MethodSignature signature = (MethodSignature) (); Method method = (); String[] parameterNames = (method); Map<String, Object> paramMap = new HashMap<>(32); for (int i = 0; i < ; i++) { (parameterNames[i], args[i]); } return paramMap; }
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.