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.
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
   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=""
<context:component-scan base-package="demo1"></context:component-scan>

#2.3 implementation of AOP

@Aspect//Enhanced annotation
public class user {
    @Before(value="execution(* demo1.userdao.add(..))")
    public void before(){
    @AfterReturning(value = "execution(* demo1.userdao.add(..))")
    public  void AfterReturning(){
    @After(value="execution(* demo1.userdao.add(..))")
    public  void after(){
    @AfterThrowing(value = "execution(* demo1.userdao.add(..)")
    public  void AfterThrowing(){

    @Around(value = "execution(* demo1.userdao.add(..))")
        public void around(ProceedingJoinPoint proceedingJoinPoint) throws
        Throwable {
            System.out.println("Before surround.........");
            //Enhanced method execution
            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() {

#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.

public class PersonProxy

Keywords: Java Spring Back-end

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