Implementation of aop annotation in Spring

1. What is AOP

AOP is a feature of java's spring framework. AOP is the abbreviation of aspect oriented programming. What is aspect oriented programming?
Aspect oriented programming is to enhance the functions of the source code without modifying the source code.
Examples
Originally, a program can realize the function of user login, and then I want to add a new function based on this function, that is, the judgment of user level. According to the original method, we need to modify the code of the original program to realize the increase of this function. Now we can realize the increase of functions without modifying the source code through AOP.

2. How to implement AOP

#2.1AOP related notes

   AOP Notes for
   @Aspect    
   Pre annotation, the annotated method will be executed before the target method
   @Before(value="execution(* demo1.userdao.add(..))")
   Post annotation: the annotated method will be executed after the target method is executed
   @AfterReturning(value = "execution(* demo1.userdao.add(..))")
   Around annotations, annotated methods will be executed before and after
   @Around(value = "execution(* demo1.userdao.add(..))")
   Exception annotation: the annotated method will be executed only when the target method has an exception
   @AfterThrowing(value = "execution(* demo1.userdao.add(..)")
   Finally, the annotated method will be executed after all annotation methods are executed
   @After(value="execution(* demo1.userdao.add(..))")

#2.2 xml configuration of AOP

Two namespaces need to be introduced, one is the context of annotation scanning, and the other is the aop namespace that implements aop
Corresponding paths need to be added in sxi: schemalocation

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="demo1"></context:component-scan>
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

#2.3 implementation of AOP

@Aspect//Enhanced annotation
public class user {
    @Before(value="execution(* demo1.userdao.add(..))")
    public void before(){
        System.out.println("======Front======");
    }
    @AfterReturning(value = "execution(* demo1.userdao.add(..))")
    public  void AfterReturning(){
        System.out.println("========Postposition=======");
    }
    @After(value="execution(* demo1.userdao.add(..))")
    public  void after(){
        System.out.println("========final=======");
    }
    @AfterThrowing(value = "execution(* demo1.userdao.add(..)")
    public  void AfterThrowing(){
        System.out.println("========abnormal=======");
    }

    @Around(value = "execution(* demo1.userdao.add(..))")
        public void around(ProceedingJoinPoint proceedingJoinPoint) throws
        Throwable {
            System.out.println("Before surround.........");
            //Enhanced method execution
            proceedingJoinPoint.proceed();
            System.out.println("After surround.........");
        }
    }

#2.4 publicity of the same entry point

The method that needs to be enhanced is called a pointcut. If a pointcut needs to be enhanced multiple times, it is to use multiple annotations to enhance the method. We can make the pointcut expression, that is, the path (value = "execution(* demo1.userdao.add(...))"), public, and set a method to obtain the path, You can get the path directly.

//Same pointcut extraction
@Pointcut(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))")
public void pointdemo() {
}
//Before advice 
//@The Before annotation indicates as a pre notification
@Before(value = "pointdemo()")
public void before() {
 System.out.println("before.........");
}

#2.5 setting the priority of multiple enhancement classes

When multiple classes enhance the same method, we can set the execution Order of these classes by adding the @ Order annotation. The smaller the number in (), the higher the priority.

@Component
@Aspect
@Order(1)
public class PersonProxy

Keywords: Java Spring Back-end

Added by MNSarahG on Mon, 28 Feb 2022 14:54:14 +0200