• Spring--AOP,代理模式,基于注解的AOP和基于XML的AOP


    AOP

    场景模拟

    模拟计算功能

    声明接口

    public interface Calculator {
        int add(int i,int j);
        int sub(int i,int j);
        int mul(int i,int j);
        int div(int i,int j);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    创建实现类

    package com.bijing.spring.proxy.impl;
    
    import com.bijing.spring.proxy.Calculator;
    
    /**
     * @author 毕晶
     * @date 2022/11/13 17:28
     */
    public class CalculatorImpl implements Calculator {
        @Override
        public int add(int i, int j) {
            int result = i + j;
            System.out.println("方法内部,result:" + result);
            return result;
        }
    
        @Override
        public int sub(int i, int j) {
            int result = i - j;
            System.out.println("方法内部,result:" + result);
            return result;
        }
    
        @Override
        public int mul(int i, int j) {
            int result = i * j;
            System.out.println("方法内部,result:" + result);
            return result;
        }
    
        @Override
        public int div(int i, int j) {
            int result = i / j;
            System.out.println("方法内部,result:" + result);
            return result;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    创建带日志功能的实现类

    package com.bijing.spring.proxy.impl;
    
    import com.bijing.spring.proxy.Calculator;
    
    /**
     * @author 毕晶
     * @date 2022/11/13 17:28
     */
    public class CalculatorImpl implements Calculator {
        @Override
        public int add(int i, int j) {
            System.out.println("日志,方法:add,参数:" + i + "," + j);
            int result = i + j;
            System.out.println("方法内部,result:" + result);
            System.out.println("日志,方法:add,结果:" + result);
    
            return result;
        }
    
        @Override
        public int sub(int i, int j) {
            System.out.println("日志,方法:sub,参数:" + i + "," + j);
            int result = i - j;
            System.out.println("方法内部,result:" + result);
            System.out.println("日志,方法:sub,结果:" + result);
    
            return result;
        }
    
        @Override
        public int mul(int i, int j) {
            System.out.println("日志,方法:mul,参数:" + i + "," + j);
            int result = i * j;
            System.out.println("方法内部,result:" + result);
            System.out.println("日志,方法:mul,结果:" + result);
    
            return result;
        }
    
        @Override
        public int div(int i, int j) {
            System.out.println("日志,方法:div,参数:" + i + "," + j);
            int result = i / j;
            System.out.println("方法内部,result:" + result);
            System.out.println("日志,方法:div,结果:" + result);
    
            return result;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    提出问题

    1. 现有代码的缺陷
      针对带日志功能的实现类,我们发现如下缺陷
    • 对核心业务功能有干扰,导致程序员在开发的时候分散了精力
    • 附加功能分散在各个业务功能方法中,不利于统一维护
    1. 解决思路
      解决这两个问题,核心就是:解耦。我们需要把附加功能从业务功能代码中抽取出来
    2. 困难
      解决问题的困难:要抽取的代码在方法内部,靠以前把子类中的重复代码抽取到父类的方式没法解决。所以需要引入新的技术。

    代理模式

    概念

    1. 介绍
      二十三种设计模式中的一种,屬于结构型模式。它的作用就是通过提供一个代理类,让我们在调用目标方法的时候,不再是直接对目标方法进行调用,而是通过代理类间接调用。让不属于目标方法核心逻辑的代码从目标方法中剥离出来一一解耦。调用目标方法时先调用代理对象的方法,减少对目标方法的调用和打扰,同时让附加功能能够集中在一起也有利于统一维护。
    2. 生活中的代理
    • 广告商找明星拍广告需要经过经纪人
    • 合作伙伴找老板谈合作要约见面时间要经过秘书
    • 房产中介是买卖双方的代理
    1. 相关术语
    • 代理:将非核心逻辑剥离出来,封装这些非核心逻辑的类、对象、方法
    • 目标:被代理“套用”了非核心逻辑代码的类、对象、方法

    静态代理

    package com.bijing.spring.proxy;
    
    import com.bijing.spring.proxy.impl.CalculatorImpl;
    
    /**
     * @author 毕晶
     * @date 2022/11/13 20:37
     */
    //需要和目标类实现同一个接口
    public class CalculatorStaticProxy implements Calculator {
    
        //要代理的对象
        private CalculatorImpl target;
    
        public CalculatorStaticProxy(CalculatorImpl target) {
            this.target = target;
        }
    
        public CalculatorImpl getTarget() {
            return target;
        }
    
        public void setTarget(CalculatorImpl target) {
            this.target = target;
        }
    
        @Override
        public int add(int i, int j) {
        //附加功能由代理类中的代理方法实现
            System.out.println("日志,方法:add,参数:" + i + "," + j);
            
        //通过目标对象来实现核心业务逻辑
            int result = target.add(i, j);
            System.out.println("日志,方法:add,结果:" + result);
            return result;
        }
    
        @Override
        public int sub(int i, int j) {
            System.out.println("日志,方法:sub,参数:" + i + "," + j);
            int result = target.sub(i, j);
            System.out.println("日志,方法:sub,结果:" + result);
            return result;
        }
    
        @Override
        public int mul(int i, int j) {
            System.out.println("日志,方法:mul,参数:" + i + "," + j);
            int result = target.mul(i, j);
            System.out.println("日志,方法:mul,结果:" + result);
            return result;
        }
    
        @Override
        public int div(int i, int j) {
            System.out.println("日志,方法:div,参数:" + i + "," + j);
            int result = target.div(i, j);
            System.out.println("日志,方法:div,结果:" + result);
            return result;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    package com.bijing.proxy;
    
    import com.bijing.spring.proxy.CalculatorStaticProxy;
    import com.bijing.spring.proxy.impl.CalculatorImpl;
    import org.junit.jupiter.api.Test;
    
    /**
     * @author 毕晶
     * @date 2022/11/13 20:46
     */
    public class ProxyTest {
    
        @Test
        public void testProxy() {
            CalculatorStaticProxy proxy = new CalculatorStaticProxy(new CalculatorImpl());
            proxy.add(199, 1);
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    静态代理确实实现了解耦,但是由于代码都写死了,完全不具备任何的灵活性。就拿日志功能来说,将来其他地方也需要附加日志,那还得再声明更多个静态代理类,那就产生了大量重复的代码,日志功能还是分散的,没有统一管理。

    提出进一步的需求:将日志功能集中到一个代理类中,将来有任何日志需求,都通过这—个代理类来实现。

    这就需要使用动态代理技术了。

    动态代理

    package com.bijing.spring.proxy;
    
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;
    import java.util.Arrays;
    
    /**
     * @author 毕晶
     * @date 2022/11/13 21:19
     */
    public class ProxyFactory {
        //创建目标对象
        private Object target;
    
        public ProxyFactory(Object target) {
            this.target = target;
        }
    
        public Object getProxy() {
            /**
             * ClassLoader loader:指定加载动态生成代理类的类加载器
             * Class[] interfaces:获取目标对象实现的所有的接口的class对象数组
             * InvocationHandler h:设置代理类中的抽象方法如何重写
             */
            ClassLoader classLoader = this.getClass().getClassLoader();
            Class<?>[] interfaces = target.getClass().getInterfaces();
            //代理对象的所有方法都会经过下列操作
            InvocationHandler h = new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    System.out.println("日志,方法:" + method.getName() + ",参数:" + Arrays.toString(args));
                    //proxy表示代理对象,method表示要执行的方法,args表示要执行的方法的参数列表
                    Object result = method.invoke(target, args);
                    System.out.println("日志,方法:" + method.getName() + ",结果:" + result);
                    return result;
                }
            };
    
            return Proxy.newProxyInstance(classLoader, interfaces, h);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    测试

    package com.bijing.proxy;
    
    import com.bijing.spring.proxy.Calculator;
    import com.bijing.spring.proxy.ProxyFactory;
    import com.bijing.spring.proxy.impl.CalculatorImpl;
    import org.junit.jupiter.api.Test;
    
    /**
     * @author 毕晶
     * @date 2022/11/13 20:46
     */
    public class ProxyTest {
    
        /**
         * 动态代理有两种:
         * 1.jdk代理,要求必须有接口,最终生成的代理类和目标类实现相同的接口,在com.sun.proxy包下,类名为$proxy2
         * 2.cglib代理,最终生成的代理类会继承目标类,并且和目标类在相同的包下
         */
        @Test
        public void testProxy() {
    
    //        CalculatorStaticProxy proxy = new CalculatorStaticProxy(new CalculatorImpl());
    //        proxy.add(199, 1);
    
            ProxyFactory proxyFactory = new ProxyFactory(new CalculatorImpl());//在proxyFactory中指定了要代理的对象是CalculatorImpl
            Calculator proxy = (Calculator) proxyFactory.getProxy();
            proxy.add(99, 1);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    AOP概念及相关术语

    概述

    AOP (Aspect Oriented Programming)是一种设计思想,是软件设计领域中的面向切面编程,它是面向对象编程的一种补充和完善,它以通过预编译方式和运行期动态代理方式实现在不修改源代码的情况下给程序动态统一添加额外功能的一种技术。

    相关术语

    1. 横切关注点

    从每个方法中抽取出来的同一类非核心业务。在同一个项目中,我们可以使用多个横切关注点对相关方法进行多个
    不同方面的增强。

    这个概念不是语法层面天然存在的,而是根据附加功能的逻辑上的需要:有十个附加功能,就有十个横切关注点。

    1. 通知

    横切关注点是针对目标对象来说的,通知是针对切面来说的。

    每一个横切关注点上要做的事情都需要写一个方法来实现,这样的方法就叫通知方法。

    • 前置通知:在被代理的目标方法执行
    • 返回通知:在被代理的目标方法成功结束后执行 (寿终正寝
    • 异常通知:在被代理的目标方法异常结束后执行(死于非命
    • 后置通知:在被代理的目标方法最终结束后执行(盖棺定论
    • 环绕通知:使用try…catch…finally结构围绕整个被代理的目标方法,包括上面四种通知对应的所有位置
    1. 切面

    封装通知方法的类

    1. 目标

    被代理的目标对象

    1. 代理

    向目标对象应用通知之后创建的代理对象,就是为目标对象创建的代理对象

    1. 连接点

    这也是一个纯逻辑概念,不是语法定义的

    把方法排成一排,每一个横向位置看成x轴方向,把方法从上到下执行的顺序看成y轴,x轴和y轴的交叉点就是连接点

    就是抽取横切关注点的位置

    在这里插入图片描述

    1. 切入点
      定位连接点的方式。
      每个类的方法中都包含多个连接点,所以连接点是类中客观存在的事物(从逻辑上来说)。
      如果把连接点看作数据库中的记录,那么切入点就是查询记录的 SQL 语句。
      Spring 的AOP 技术可以通过切入点定位到特定的连接点。
      切点通过 org.springframework.aop.Pointcut 接口进行描述,它使用类和方法作为连接点的查询条件。

    从目标对象中把非核心业务代码即横切关注点抽取出来,放在切面中,在切面中每一个横切关注点都是一个方法,又叫通知,把切面中的通知,通过切入点表达式套到连接点上

    作用

    • 简化代码:把方法中固定位置的重复的代码抽取出来,让被抽取的方法更专注于自己的核心功能,提高内聚性。
    • 代码增强:把特定的功能封装到切面类中,看哪里有需要,就往上套,被套用了切面逻辑的方法就被切面给增强了

    基于注解的AOP

    技术说明

    在这里插入图片描述

    • 动态代理 (InvocationHandler):JDK原生的实现方式,需要被代理的目标类必须实现接口。因为这个技术
      要求代理对象和目标对象实现同样的接口 (兄弟两个拜把子模式)。
    • cglib:通过继承被代理的目标类(认干爹模式) 实现代理,所以不需要目标类实现接口。
    • AspectJ:本质上是静态代理,将代理逻辑”织入"被代理的目标类编译得到的字节码文件,所以最终效果是动态的。weaver就是织入器。Spring只是借用了AspectJ中的注解。

    准备工作

    1. 添加依赖
      pom.xml
        <dependencies>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-contextartifactId>
                <version>5.3.1version>
            dependency>
            <dependency>
                <groupId>junitgroupId>
                <artifactId>junitartifactId>
                <version>4.13.2version>
                <scope>testscope>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-aspectsartifactId>
                <version>5.3.23version>
            dependency>
        dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    1. 准备被代理的目标资源
    public interface Calculator {
        int add(int i,int j);
        int sub(int i,int j);
        int mul(int i,int j);
        int div(int i,int j);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    package com.bijing.spring.aop.annotation;
    
    import org.springframework.stereotype.Component;
    
    /**
     * @author 毕晶
     * @date 2022/11/13 17:28
     */
    @Component
    public class CalculatorImpl implements Calculator {
        @Override
        public int add(int i, int j) {
            int result = i + j;
            System.out.println("方法内部,result:" + result);
            return result;
        }
    
        @Override
        public int sub(int i, int j) {
            int result = i - j;
            System.out.println("方法内部,result:" + result);
            return result;
        }
    
        @Override
        public int mul(int i, int j) {
            int result = i * j;
            System.out.println("方法内部,result:" + result);
            return result;
        }
    
        @Override
        public int div(int i, int j) {
            int result = i / j;
            System.out.println("方法内部,result:" + result);
            return result;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    创建切面类并配置

    package com.bijing.spring.aop.annotation;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.Signature;
    import org.aspectj.lang.annotation.*;
    import org.springframework.stereotype.Component;
    
    import java.util.Arrays;
    
    /**
     * @author 毕晶
     * @date 2022/11/14 12:00
     * **
     * 1.在切面中,需要通过指定的注解将方法标识为通知方法
     * @Before:前置通知,在目标对象方法执行之前执行 .
     * @After:后置通知,在目标对象方法的finally子句中执行 .
     * @AfterReturning:返回通知,在目标对象方法返回值之后执行 .
     * @AfterThrowing:异常通知,在目标对象方法的catch子句中执行 .
     * **
     * 2.切入点表达式:设置在标识通知的注解的value属性中
     * execution(public int com.bijing.spring.aop.annotation.CalculatorImpl.add(int,int))
     * execution(* com.bijing.spring.aop.annotation.CalculatorImpl.*(..))
     * 第一个*表示任意的访问修饰符和返回值类型
     * 第二个*表示类中的任意方法
     * ..表示任意的参数列表
     * 类的地方也可以使用*表示包下所有的类
     * **
     * 3.重用切入点表达式
     * //@Pointcut声明一个公共的切入点表达式
     * .     @Pointcut("execution(* com.bijing.spring.aop.annotation.CalculatorImpl.*(..))")
     * .     public void pointCut() {}
     * 使用方式:    @After("pointCut()")
     * **
     * 4.获取连接点信息
     * 在通知方法的参数位置,设置joinPoint类型的参数,就可以获取连接点所对应方法的信息
     * //signature获取连接点所对应的方法的签名信息
     * Signature signature = joinPoint.getSignature();
     * //获取连接点所对应方法的参数
     * Object[] args = joinPoint.getArgs();
     * **
     * 5.切面的优先级可以通过@Order注解的value属性来设置优先级,默认值为Integer的最大值
     * @Order注解的value属性值越小,优先级越高 .
     */
    @Component
    @Aspect//将当前组件标识为切面组件
    public class LoggerAspect {
    
        @Pointcut("execution(* com.bijing.spring.aop.annotation.CalculatorImpl.*(..))")
        public void pointCut() {
        }
    
        //    @Before("execution(public int com.bijing.spring.aop.annotation.CalculatorImpl.add(int,int))")
        @Before("execution(* com.bijing.spring.aop.annotation.CalculatorImpl.*(..))")
        public void beforeAdviceMethod(JoinPoint joinPoint) {//JoinPoint表示连接点
            //signature获取连接点所对应的方法的签名信息
            Signature signature = joinPoint.getSignature();
            //获取连接点所对应方法的参数
            Object[] args = joinPoint.getArgs();
            System.out.println("LoggerAspect,方法:" + signature.getName() + ",参数:" + Arrays.toString(args));
        }
    
        @After("pointCut()")
        public void afterAdviceMethod(JoinPoint joinPoint) {
            //signature获取连接点所对应的方法的签名信息
            Signature signature = joinPoint.getSignature();
            //获取连接点所对应方法的参数
            Object[] args = joinPoint.getArgs();
            System.out.println("LoggerAspect,方法:" + signature.getName() + ",执行完毕");
        }
    
        /*    在返回通知中,若要获取目标对象方法的返回值,
             只需要通过@AfterReturnning注解的returning属性,
             就可以将通知方法的某个参数指定为接收目标对象方法的返回值的参数*/
        @AfterReturning(value = "pointCut()", returning = "result")
        public void afterReturningAdviceMethod(JoinPoint joinPoint, Object result) {
            //signature获取连接点所对应的方法的签名信息
            Signature signature = joinPoint.getSignature();
            System.out.println("LoggerAspect,方法:" + signature.getName() + ",结果:" + result);
        }
    
        /*    在异常通知中,若要获取目标对象方法的异常,
         只需要通过@AfterThrowing注解的throwing属性,
         就可以将通知方法的某个参数指定为接收目标对象方法的出现异常的参数*/
        @AfterThrowing(value = "pointCut()", throwing = "exception")
        public void afterThrowingAdviceMethod(JoinPoint joinPoint, Exception exception) {
            //signature获取连接点所对应的方法的签名信息
            Signature signature = joinPoint.getSignature();
            System.out.println("LoggerAspect,方法:" + signature.getName() + ",异常通知:" + exception);
        }
    
        @Around("pointCut()")
        //环绕通知的返回值一定要和目标对象方法的返回值一致
        public Object aroundAdviceMethod(ProceedingJoinPoint proceedingJoinPoint) {
            Object result = null;
            try {
                System.out.println("环绕通知-->前置通知位置");
                //标识目标对象方法的执行
                result = proceedingJoinPoint.proceed();
                System.out.println("环绕通知-->返回通知位置");
            } catch (Throwable e) {
                e.printStackTrace();
                System.out.println("环绕通知-->异常通知位置");
            } finally {
                System.out.println("环绕通知-->后置通知位置");
            }
            return result;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110

    aop-annotation.xml

    
        <context:component-scan base-package="com.bijing.spring.aop.annotation">context:component-scan>
    
    
        <aop:aspectj-autoproxy>aop:aspectj-autoproxy>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    各种通知

    • 前置通知:使用@Before注解标识,在被代理的目标方法执行
    • 返回通知:使用@AfterReturning注解标识,在被代理的目标方法成功结束后执行 (寿终正寝
    • 异常通知:使用@AfterException注解标识,在被代理的目标方法异常结束后执行(死于非命
    • 后置通知:使用@After注解标识,在被代理的目标方法最终结束后执行(盖棺定论
    • 环绕通知:使用@Around注解标识,使用try…catch…finally结构围绕整个被代理的目标方法,包括上面四种通知对应的所有位置
    各种通知的执行顺序
    • Spring版本5.3.x以前:
      • 前置通知
      • 目标操作
      • 后置通知
      • 返回通知或异常通知
    • Spring版本5.3.x以后:
      • 前置通知
      • 目标操作
      • 返回通知或异常通知
      • 后置通知

    切入点表达式语法

    1. 作用
      在这里插入图片描述

    2. 语法细节

    • 用*号代替”权限修饰符’和“返回值"部分表示’权限修饰符"和”返回值”不限
    • 在包名的部分,—个"*"号只能代表包的层次结构中的一层,表示这一层是任意的。
      • 例如:*.Hello匹配com.Hello,不匹配com.bijing.Hello
    • 在包名的部分,使用以"*…"表示包名任意、包的层次深度任意
    • 在类名的部分,类名部分整体用*号代替,表示类名任意
    • 在类名的部分,可以使用*号代替类名的一部分
      • 例如:*Service匹配所有名称以Service结尾的类或接口
    • 在方法名部分,可以使用*号表示方法名任意
    • 在方法名部分,可以使用*号代替方法名的一部分
      • 例如:*Operation匹配所有方法名以Operation结尾的方法
    • 在方法参数列表部分,使用(…)表示参数列表任意
    • 在方法参数列表部分,使用(int,…)表示参数列表以一个int类型的参数开头
    • 在方法参数列表部分,基本数据类型和对应的包装类型是不一样的
      • 切入点表达式中使用 int 和实际方法中 Integer 是不匹配的
    • 在方法返回值部分,如果想要明确指定一个返回值类型,那么必须同时写明权限修饰符
      • 例如:execution(public int …Service.*(…, int)) 正确
      • 例如: execution(* int …Service.*(…, int)) 错误
        在这里插入图片描述

    重用切入点表达式

    1. 声明
    @Pointcut("execution(* com.bijing.aop.annotation.*.*(..))")
    public void pointCut(){}
    
    • 1
    • 2
    1. 在同一个切面中使用
    @Before("pointCut()")
    public void beforeMethod(JoinPoint joinPoint){
    String methodName = joinPoint.getSignature().getName();
    String args = Arrays.toString(joinPoint.getArgs()); System.out.println("Logger-->前置通知,方法名:"+methodName+",参数:"+args);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. 在不同切面中使用
    @Before("com.bijing.aop.CommonPointCut.pointCut()")
    public void beforeMethod(JoinPoint joinPoint){
    String methodName = joinPoint.getSignature().getName();
    String args = Arrays.toString(joinPoint.getArgs()); System.out.println("Logger-->前置通知,方法名:"+methodName+",参数:"+args);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    获取通知相关信息

    1. 获取连接点信息

    获取连接点信息可以在通知方法的参数位置设置JoinPoint类型的形参

        @Before("execution(* com.bijing.spring.aop.annotation.CalculatorImpl.*(..))")
        public void beforeAdviceMethod(JoinPoint joinPoint) {//JoinPoint表示连接点
            //signature获取连接点所对应的方法的签名信息
            Signature signature = joinPoint.getSignature();
            //获取连接点所对应方法的参数
            Object[] args = joinPoint.getArgs();
            System.out.println("LoggerAspect,方法:" + signature.getName() + ",参数:" + Arrays.toString(args));
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    1. 获取目标方法的返回值

    @AfterReturning中的属性returning,用来将通知方法的某个形参,接收目标方法的返回值

        @AfterReturning(value = "pointCut()", returning = "result")
        public void afterReturningAdviceMethod(JoinPoint joinPoint, Object result) {
            //signature获取连接点所对应的方法的签名信息
            Signature signature = joinPoint.getSignature();
            System.out.println("LoggerAspect,方法:" + signature.getName() + ",结果:" + result);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    1. 获取目标方法的异常

    @AfterThrowing中的属性throwing,用来将通知方法的某个形参,接收目标方法的异常

        @AfterThrowing(value = "pointCut()", throwing = "exception")
        public void afterThrowingAdviceMethod(JoinPoint joinPoint, Exception exception) {
            //signature获取连接点所对应的方法的签名信息
            Signature signature = joinPoint.getSignature();
            System.out.println("LoggerAspect,方法:" + signature.getName() + ",异常通知:" + exception);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    环绕通知

     @Around("pointCut()")
        //环绕通知的返回值一定要和目标对象方法的返回值一致
        public Object aroundAdviceMethod(ProceedingJoinPoint proceedingJoinPoint) {
            Object result = null;
            try {
                System.out.println("环绕通知-->前置通知位置");
                //标识目标对象方法的执行
                result = proceedingJoinPoint.proceed();
                System.out.println("环绕通知-->返回通知位置");
            } catch (Throwable e) {
                e.printStackTrace();
                System.out.println("环绕通知-->异常通知位置");
            } finally {
                System.out.println("环绕通知-->后置通知位置");
            }
            return result;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    AOPByAnnotationTest
    为什么在bean文件中注入的是实现类,但是通过getBean()取出的时候却必须强制转化为接口类?

    package com.bijing.spring.test;
    
    import com.bijing.spring.aop.annotation.Calculator;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * @author 毕晶
     * @date 2022/11/14 14:02
     */
    public class AOPByAnnotationTest {
        @Test
        public void testAOPByAnnotation() {
            ApplicationContext ioc = new ClassPathXmlApplicationContext("aop-annotation.xml");
            /*        为什么在bean文件中注入的是实现类,但是通过getBean()取出的时候却必须强制转化为接口类?
            如果被代理的目标对象实现了至少一个接口,则会使用JDK动态代理。
            若该目标对象没有实现任何接口,则创建一个Cglib代理,以目标对象子类的方式类实现代理.
            使用Cglib代理的时候,通过getBean()取出的注入对象既可以是普通对象,也可以是接口,通过JDK动态代理就只能使用接口。*/
            Calculator calculator = ioc.getBean(Calculator.class);
            calculator.div(10, 1);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    切面的优先级

    相同目标方法上同时存在多个切面时,切面的优先级控制切面的内外嵌套顺序

    • 优先级高的切面:外面
    • 优先级低的切面:里面

    使用@Order注解可以控制切面的优先级:

    • @Order(较小的数):优先级高
    • @Order(较大的数):优先级低

    基于XML的AOP(了解)

    准备工作

    参考基于注解的AOP环境

    实现

    
    <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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
    
        <context:component-scan base-package="com.bijing.spring.aop.xml">context:component-scan>
        <aop:config>
            
            <aop:pointcut id="pointCut" expression="execution(* com.bijing.spring.aop.xml.CalculatorImpl.* (..))"/>
            
            <aop:aspect ref="loggerAspect">
                <aop:before method="beforeAdviceMethod" pointcut-ref="pointCut">aop:before>
                <aop:after method="afterAdviceMethod" pointcut-ref="pointCut">aop:after>
                <aop:after-returning method="afterReturningAdviceMethod" pointcut-ref="pointCut"
                                     returning="result">aop:after-returning>
                <aop:after-throwing method="afterThrowingAdviceMethod" pointcut-ref="pointCut"
                                    throwing="exception">aop:after-throwing>
                <aop:around method="aroundAdviceMethod" pointcut-ref="pointCut">aop:around>
            aop:aspect>
        aop:config>
    
        <aop:config>
            <aop:aspect ref="validateAspect" order="1">
                <aop:before method="beforeMethod" pointcut-ref="pointCut">aop:before>
            aop:aspect>
        aop:config>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
  • 相关阅读:
    架构师选择题--数据库技术
    【Bluetooth蓝牙开发】六、BLE协议之物理层浅析
    学习如何使用最强大的 JavaScript 函数
    Linux之vim的使用详细解析
    记一次冲突:JsonSerialize注解标注的字段返回两次的错误
    Helm 基础入门 Helm介绍与安装
    基于Unity3D实现的HitUFO鼠标打飞碟游戏
    将文件移动到ubuntu/linux/centos虚拟机上
    温故知新(十一)——IIC
    【力扣hot100】刷题笔记Day17
  • 原文地址:https://blog.csdn.net/weixin_43903745/article/details/127836448