Spring - what is AOP? How to use it?

1. What is AOP?

Add functionality without modifying the source code

2. What is the bottom layer?

dynamic proxy

aop is an extension function of IOC. The existing IOC and AOP are just a new extension point in the whole process of IOC: BeanPostProcessor

There is a step in the bean creation process that can extend the bean. AOP itself is an extension function, so the post-processing method of BeanPostProcessor is implemented

3. Terminology

①, connection point

Which methods in a class can be enhanced, these methods are called connection points

②, entry point

The methods that are actually enhanced are called pointcuts

③. Notification (Enhanced)

The part of the logic that is actually enhanced is called a notification (enhancement)

There are many types of notifications

  • advance notice

  • post notification

  • surround notification

  • Exception notification

  • final notice

④, section

The process of applying advice to a pointcut


Four, AOP operation

The Spring framework is generally based on AspectJ to implement AOP operations

What is AspectJ?

It is not a part of Spring, and is an independent AOP framework. Generally, AspectJ and Spring framework are used together to perform AOP operations.

Realize AOP operation based on AspectJ

  1. Based on xml configuration

  1. Annotation-based

1. Annotation method

Introduce AOP dependency

Enable Annotation Scanning

Turn on Aspect to generate proxy objects

User class

package com.atguigu.spring5.AOP;

import org.springframework.stereotype.Component;

/**
 * @BelongsProject: 02-Spring
 * @BelongsPackage: com.atguigu.spring5.AOP
 * @Author: dengLiMei
 * @CreateTime: 2023-02-11  19:54
 * @Description: TODO
 * @Version: 1.0
 */
//被增强类
@Component
public class User {
    public void add() {
        System.out.println("add……");
    }
}

UserProxy class

package com.atguigu.spring5.AOP;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @BelongsProject: 02-Spring
 * @BelongsPackage: com.atguigu.spring5.AOP
 * @Author: dengLiMei
 * @CreateTime: 2023-02-11  19:55
 * @Description: TODO
 * @Version: 1.0
 */
//增强的类
@Component
@Aspect   //生成代理对象
public class UserProxy {
    //前置通知
    //@Before注解表示作为前置通知
    @Before(value = "execution(* com.atguigu.spring5.AOP.User.add())")
    public void before() {
        System.out.println("before.....");
    }

    //最终通知:方法之后就执行;有异常也执行
    @After(value = "execution(* com.atguigu.spring5.AOP.User.add())")
    public void After() {
        System.out.println("After.....");
    }

    //后置通知(返回通知):在返回结果之后执行:有异常不执行
    @AfterReturning(value = "execution(* com.atguigu.spring5.AOP.User.add())")
    public void AfterReturning() {
        System.out.println("AfterReturning.....");
    }

    //异常通知:有异常了才执行,没异常不执行
    @AfterThrowing(value = "execution(* com.atguigu.spring5.AOP.User.add())")
    public void AfterThrowing() {
        System.out.println("AfterThrowing.....");
    }

    //环绕通知
    //表示在方法之前和方法之后都执行
    @Around(value = "execution(* com.atguigu.spring5.AOP.User.add())")
    public void Around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕之前.....");

        //被增强的方法执行
        proceedingJoinPoint.proceed();

        System.out.println("环绕之后.....");
    }
}

Main class

package com.atguigu.spring5.AOP;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @BelongsProject: 02-Spring
 * @BelongsPackage: com.atguigu.spring5.AOP
 * @Author: dengLiMei
 * @CreateTime: 2023-02-11  20:05
 * @Description: TODO
 * @Version: 1.0
 */
public class Main {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("aopbean.xml");
        User user = context.getBean("user", User.class);
        user.add();
    }
}

Output result:

Question: Enhance the specified method of the specified class

Solution: extract the same entry point

PersonProxy class

Add a new class based on the above example

package com.atguigu.spring5.AOP;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @BelongsProject: 02-Spring
 * @BelongsPackage: com.atguigu.spring5.AOP
 * @Author: dengLiMei
 * @CreateTime: 2023-02-11  20:21
 * @Description: TODO
 * @Version: 1.0
 */
@Component
@Aspect
@Order(1)
public class PersonProxy {
    //后置通知(返回通知)
    @Before(value = "execution(* com.atguigu.spring5.AOP.User.add(..))")
    public void afterReturning(){
        System.out.println("Person Before……");
    }
}

Output result:

There are multiple enhancement classes to enhance the same method, set the priority of the enhancement class

Add the annotation @Order (number type value) on the enhanced class, the smaller the number type value, the higher the priority

Two, AspectJ configuration method

create object

Configure aop enhancement

entry point

configuration aspect

Book class

package com.atguigu.spring5.AOP.AOPConfigue;

/**
 * @BelongsProject: 02-Spring
 * @BelongsPackage: com.atguigu.spring5.AOP.AOPConfigue
 * @Author: dengLiMei
 * @CreateTime: 2023-02-11  20:38
 * @Description: TODO
 * @Version: 1.0
 */
public class Book {
    public void buy(){
        System.out.println("买");
    }
}

BookProxy class

package com.atguigu.spring5.AOP.AOPConfigue;

/**
 * @BelongsProject: 02-Spring
 * @BelongsPackage: com.atguigu.spring5.AOP.AOPConfigue
 * @Author: dengLiMei
 * @CreateTime: 2023-02-11  20:40
 * @Description: TODO
 * @Version: 1.0
 */
public class BookProxy {
    public void before(){
        System.out.println("before……");
    }
}

Main class

package com.atguigu.spring5.AOP.AOPConfigue;

import com.atguigu.spring5.AOP.AOPAnnotation.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @BelongsProject: 02-Spring
 * @BelongsPackage: com.atguigu.spring5.AOP.AOPConfigue
 * @Author: dengLiMei
 * @CreateTime: 2023-02-11  20:44
 * @Description: TODO
 * @Version: 1.0
 */
public class Main {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("aopbean2.xml");
        Book book = context.getBean("book", Book.class);
        book.buy();
    }
}

Output result:


Spring series articles:

Spring - what is it? effect? content? Design pattern used?

Spring——Bean management-xml method for attribute injection

Spring——Bean management-annotation method for attribute injection

Spring - what is IOC?

Spring - what is AOP? How to use it?

Spring - what is a transaction? Communication behavior? What are the transaction isolation levels?

Spring - Integrate junit4, junit5 usage

如果有想要交流的内容欢迎在评论区进行留言,如果这篇文档受到了您的喜欢那就留下你点赞+收藏脚印支持一下博主~

Guess you like

Origin blog.csdn.net/weixin_43319713/article/details/129338954