Use of AOP in Springboot

Aspect Oriented Programming (Aspect Oriented Programming) is the product of the development of software programming ideas to a certain stage, and is a useful complement to object-oriented programming.AOP is generally used in situations with crosscutting logic, such as access control, transaction management, performance detection, and so on.

Logging, exception handling, transaction control, and so on are all necessary for a robust business system.However, in order to keep the system robust and available, it is necessary to write similar code repeatedly in many business methods, which makes the already complex business processing code more complex.Developers of business functions also need to check twice whether these extra codes are handled correctly, whether there are omissions, and if they need to modify the format of log information or rules for security validation, or add additional auxiliary functions, they will result in frequent and large changes to business code.

Face-oriented programming is to add new functions to code and enhance code snippets without changing the original program.His design idea comes from agent design mode.

 

 

 

1. Enhancements inserted before the original object method are preenhancements

2. Enhancement processing inserted after execution of this method is post-enhancement

3. Enhancement processing around methods is surround enhancement, which is the most powerful enhancement processing. It can get or modify the parameters, return values, exception handling, and even determine whether the target method is executed.

4. Enhancement handling when the method throws an exception is enhancement for exception throwing.

5. Final enhancements, whether the method throws an exception or exits normally, are executed, similar to the role of the finally block in exception handling mechanisms, which are generally used to free resources

 

Use annotations in Springboot

Need to introduce the required jar:spring-boot-starter-aop

Create an aop enhanced processing class

@Slf4j
@Aspect
@Component
public class LoggerAspect {

    // matching com.lzz.lzzapp.common.user All methods of all classes under package and subpackage
    @Pointcut("execution(* com.lzz.lzzapp.common.user..*.*(..))")
    public void logPointCut(){

    }
    //Pre-enhanced notifications before connection point execution
    @Before("logPointCut()")
    public void before(){
        log.info("Business method is about to be invoked");
    }
    //Ultimately enhances notifications performed after connection point execution (returns exceptions to notifications and exception notifications)
    @After("logPointCut()")
    public void after(){
        log.info("Business method call complete");
    }

    /**
     * Post-enhanced notifications performed after connection point execution (return notifications)
     */
    @AfterReturning(value="logPointCut()",returning="result")
    public void doAfterReturning(JoinPoint joinPoint, Object result){
        String methodName = joinPoint.getSignature().getName();
        log.info("call"+joinPoint.getTarget()+"Of"+methodName+"Method.Parameters:"+Arrays.toString(joinPoint.getArgs())
                +". Method return value:"+result);
    }

    /**
     * Exception enhances notifications performed after connection point execution (exception notifications)
     */
    @AfterThrowing("logPointCut()")
    public void doAfterThrowing(){
        log.info("Exception handling complete");
    }

    /**
     * Surround Enhancement
     */
    @Around("logPointCut()")
    public void doAround(ProceedingJoinPoint jp){
        log.info("call"+jp.getTarget()+"Of"+jp.getSignature().getName()+"Method.Parameters:"+Arrays.toString(jp.getArgs()));
        try {
            Object result=jp.proceed();//Execute Target Method
            log.info("Method return value:"+result);
        }catch (Throwable e){
            log.error(jp.getSignature().getName()+"Method Exception");
            e.printStackTrace();
        }
    }

}

 

Define facets with @Aspect and @Pointcut to define entry points

The execution point for the entry point matching is JointPoint, which Spring automatically injects into the instance. The getTarget() method of the joinpoint can get the proxy object, and the getSignature() method returns the target method of the proxy.The getArgs() method returns an array of parameters passed to the target method

For post-enhancements, you can also define a return value to receive the proxy method, which must be specified by the returning property in the @AfterReturning comment

execution is the entry point indicator. It is an entry point expression in parentheses that allows you to configure the method to be entered. The entry point expression supports fuzzy matching

public * addUser(com.entity.User) * indicates a return value that matches all types
 public void *(com.entity.User) * indicates that all method names match
 public void addUser(..).. Indicates matching number and type of all parameters                
* com.user. *. *(..) represents all methods that match all classes under the com.entity package
 * com.user. *. *(..) represents all methods that match all classes under the com.entity package and its subpackages

 

Keywords: Java Programming Spring SpringBoot

Added by SephirGaine on Thu, 26 Dec 2019 06:46:34 +0200