SpringAOP学习笔记

87 阅读4分钟

这是我参与11月更文挑战的第16天,活动详情查看:2021最后一次更文挑战

SpringAOP

AOP概念

1、什么是AOP(面向切面编程)

​ (1)面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

image-20211117151153239

(2)通俗的描述:不通过修改源代码方式,在主干功能里面添加新功能

AOP底层原理

2、AOP底层使用动态代理

(1)有两种情况

​ 第一种:有接口,使用JDK动态代理

​ 1、使用JDK动态代理,使用Proxy类里面的方法创建代理对象

image-20211117153433782

​ (1)调用newProxyInstance方法

​ 三个参数:第一个参数,类加载器

​ 第二个参数,增强方法所在的类,这个类实现的接口,支持多个接口

​ 第三个参数,实现这个接口InvocationHandler,创建代理对象,写增强的方法

​ 2、代码实现

​ (1)创建接口,定义方法

public interface UserDao {
    int add (int a,int b);

    String update(String id);
}

​ (2) 创建接口实现类,实现方法

package dao.impl;

import dao.UserDao;

public class UserDaoImpl implements UserDao {
    @Override
    public int add(int a, int b) {
        return a+b;
    }

    @Override
    public String update(String id) {
        return id;
    }
}

​ (3)使用Proxy类创建代理对象

public class Main {
    public static void main(String[] args) {
        // 创建接口实现类代理对象
        Class[] interfaces = {UserDao.class};

        UserDaoImpl userDao = new UserDaoImpl();
        UserDao o = (UserDao) Proxy.newProxyInstance(Main.class.getClassLoader(), interfaces, new UserDaoProxy(userDao));

        int add = o.add(1, 2);
        System.out.println(add);
    }
}
class UserDaoProxy implements InvocationHandler{

    // 1 把创建的是谁的代理对象,就把谁传过来
    // 有参构造器
    private Object object;

    public UserDaoProxy(Object object) {
        this.object = object;
    }

    // 增强的逻辑
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        // 方法执行前
        System.out.println("方法之前执行"+ object) ;
        // 被增强的方法执行
        Object invoke = method.invoke(object, args);
        // 方法之后
        System.out.println(object);


        return invoke;
    }
}

image-20211117152239927

​ 第二种:没有接口,使用CGLIB动态代理

image-20211117152705752

AOP(术语)

image-20211117164602004

AOP的操作(准备)

1、Spring框架一般基于AspectJ实现AOP操作

​ (1)啥是AspectJ :AspectJ不是Spring组成部分,独立AOP框架,一般把AspectJ和Spring框架一起使用,进行AOP操作

2、基于AspectJ实现AOP操作

(1)基于xml配置文件实现

(2)基于注解方式实现(使用)

3、在项目工程里面引入AOP相关依赖

4、切入点表达式

(1)切入点表达式的作用:知道那个类里面的那个方法进行增强

(2)语法结构:image-20211117182339641

image-20211117182555703

image-20211117182806834

image-20211117182709484

AOP操作

1、基于注解进行使用

​ (1)创建类,在类里面定义方法

package aopanno;
public class User {
    public void add(){
        System.out.println("add.........");
    }
}

​ (2)创建增强类(编写增强逻辑)

​ 在增强类里面,创建方法,让不同方法代表不同通知类型

package aopanno;
public class UserProxy {
    // 前置通知
    public void before(){
        System.out.println("before..........");
    }
}

​ (3)进行通知的配置

在spring配置文件中,开启注解扫描

<?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/beans/spring-context.xsd
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/beans/spring-aop.xsd">

    <!--开启注解扫描-->
    <context:component-scan base-package="aopanno"/>
</beans>

使用注解创建User和UserProxy对象

@Component
public class User {
    public void add(){
        System.out.println("add.........");
    }
}
@Component
public class UserProxy {
    // 前置通知
    public void before(){
        System.out.println("before..........");
    }
}

在增强类上面添加注解@Aspect

import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
// 加强的类
@Aspect // 生成代理对象 
@Component
public class UserProxy {
    // 前置通知
    public void before(){
        System.out.println("before..........");
    }
}

在spring配置文件中开启生成代理对象

<?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/beans/spring-context.xsd
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/beans/spring-aop.xsd">

    <!--开启注解扫描-->
    <context:component-scan base-package="aopanno"/>

    <!--开启Aspect生成代理对象-->
    <aop:aspectj-autoproxy/>
</beans>

配置不同类型的通知

(1)在增强类的里面,在作为通知方法上面添加通知类型注解,使用切入点表达式进行配置

// 加强的类
@Aspect // 生成代理对象
@Component
public class UserProxy {
    // 前置通知
    @Before(value = "execution(* aopanno.User.add(..))" )
    public void before(){
        System.out.println("before..........");
    }
}

PS:AOP测试不可以直接在单元测试里测试,要添加相关注解来解决这个问题(并且添加相关依赖包)

其他通知

// 加强的类
@Aspect // 生成代理对象
@Component
public class UserProxy {
    // 前置通知
    @Before(value = "execution(* aopanno.User.add(..))" )
    public void before(){
        System.out.println("before..........");
    }
    // 最终通知
    @After(value = "execution(* aopanno.User.add(..))")
    public void after(){
        System.out.println("after.........");
    }
    // 后置通知
    @AfterReturning(value = "execution(* aopanno.User.add(..))")
    public void afterReturning(){
        System.out.println("afterReturning.........");
    }
    // 异常通知
    @AfterThrowing(value = "execution(* aopanno.User.add(..))")
    public void afterThrowing(){
        System.out.println("afterThrowing.........");
    }

    // 环绕通知
    @Around(value = "execution(* aopanno.User.add(..))")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("around before.........");
        // 被增强的方法
        proceedingJoinPoint.proceed(); // 执行被加强的方法

        System.out.println("around after..............");
    }
}

5、公共切入点进行抽取

// 加强的类
@Aspect // 生成代理对象
@Component
public class UserProxy {


    // 相同切入点进行抽取
    @Pointcut(value = "execution(* aopanno.User.add(..))")
    public void pointCat(){}

    // 前置通知
    @Before(value = "pointCat()" )
    public void before(){
        System.out.println("before..........");
    }
    // 最终通知
    @After(value = "pointCat()")
    public void after(){
        System.out.println("after.........");
    }
    // 后置通知
    @AfterReturning(value = "pointCat()")
    public void afterReturning(){
        System.out.println("afterReturning.........");
    }
    // 异常通知
    @AfterThrowing(value = "pointCat()")
    public void afterThrowing(){
        System.out.println("afterThrowing.........");
    }

    // 环绕通知
    @Around(value = "pointCat()")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("around before.........");
        // 被增强的方法
        proceedingJoinPoint.proceed(); // 执行被加强的方法

        System.out.println("around after..............");
    }
}

6、多个增强类对同一个方法进行增强,设置增强类优先级

在增强类上面添加@Order(int),设置越小优先级越高

image-20211117201256701

参考内容

尚硅谷Spring5框架教程(idea版)