代码优雅升级,提升开发效率:挖掘Spring AOP配置的学习宝藏!
作者:mmseoamin日期:2023-12-27

AOP配置管理

    • 1.1 AOP切入点表达式
      • 1.1.1 语法格式
      • 1.1.2 通配符
      • 1.1.3 书写技巧
      • 1.2 AOP通知类型
        • 1.2.1 类型介绍
        • 1.2.2 环境准备
        • 1.2.3 通知类型的使用
          • 前置通知
          • 后置通知
          • 环绕通知
            • 基本使用
            • 注意事项
            • 返回后通知
            • 异常后通知
            • 通知类型总结
              • 知识点1:@After
              • 知识点2:@AfterReturning
              • 知识点3:@AfterThrowing
              • 知识点4:@Around
              • 1.3 AOP通知获取数据
                • 1.3.1 环境准备
                • 1.3.2 获取参数
                  • 非环绕通知获取方式
                  • 环绕通知获取方式
                  • 1.3.3 获取返回值
                    • 环绕通知获取返回值
                    • 返回后通知获取返回值
                    • 1.3.4 获取异常
                      • 环绕通知获取异常
                      • 抛出异常后通知获取异常
                      • 2,AOP总结
                        • 2.1 AOP的核心概念
                        • 2.2 切入点表达式
                        • 2.3 五种通知类型
                        • 2.4 通知中获取参数

                          1.1 AOP切入点表达式

                          在这里插入图片描述

                          1.1.1 语法格式

                          首先我们先要明确两个概念:

                          • 切入点:要进行增强的方法
                          • 切入点表达式:要进行增强的方法的描述方式

                            对于切入点的描述,我们其实是有两中方式的,先来看下前面的例子

                            在这里插入图片描述

                            描述方式一:执行com.test.dao包下的BookDao接口中的无参数update方法

                            execution(void com.test.dao.BookDao.update())
                            

                            描述方式二:执行com.test.dao.impl包下的BookDaoImpl类中的无参数update方法

                            execution(void com.test.dao.impl.BookDaoImpl.update())
                            

                            因为调用接口方法的时候最终运行的还是其实现类的方法,所以上面两种描述方式都是可以的。

                            对于切入点表达式的语法为:

                            • 切入点表达式标准格式:动作关键字(访问修饰符 返回值 包名.类/接口名.方法名(参数) 异常名)

                              对于这个格式,我们不需要硬记,通过一个例子,理解它:

                              execution(public User com.test.service.UserService.findById(int))
                              
                              • execution:动作关键字,描述切入点的行为动作,例如execution表示执行到指定切入点
                              • public:访问修饰符,还可以是public,private等,可以省略
                              • User:返回值,写返回值类型
                              • com.test.service:包名,多级包使用点连接
                              • UserService:类/接口名称
                              • findById:方法名
                              • int:参数,直接写参数的类型,多个类型用逗号隔开
                              • 异常名:方法定义中抛出指定异常,可以省略

                                切入点表达式就是要找到需要增强的方法,所以它就是对一个具体方法的描述,但是方法的定义会有很多,所以如果每一个方法对应一个切入点表达式,想想这块就会觉得将来编写起来会比较麻烦,有没有更简单的方式呢?

                                1.1.2 通配符

                                我们使用通配符描述切入点,主要的目的就是简化之前的配置,具体都有哪些通配符可以使用?

                                • *:单个独立的任意符号,可以独立出现,也可以作为前缀或者后缀的匹配符出现

                                  execution(public * com.test.*.UserService.find*(*))
                                  

                                  匹配com.test包下的任意包中的UserService类或接口中所有find开头的带有一个参数的方法

                                • ..:多个连续的任意符号,可以独立出现,常用于简化包名与参数的书写

                                  execution(public User com..UserService.findById(..))
                                  

                                  匹配com包下的任意包中的UserService类或接口中所有名称为findById的方法

                                • +:专用于匹配子类类型

                                  execution(* *..*Service+.*(..))
                                  

                                  这个使用率较低,描述子类的,咱们做JavaEE开发,继承机会就一次,使用都很慎重,所以很少用它。*Service+,表示所有以Service结尾的接口的子类。

                                  接下来,使用切入点表达式来分析下:

                                  在这里插入图片描述

                                  execution(void com.test.dao.BookDao.update())
                                  匹配接口,能匹配到
                                  execution(void com.test.dao.impl.BookDaoImpl.update())
                                  匹配实现类,能匹配到
                                  execution(* com.test.dao.impl.BookDaoImpl.update())
                                  返回值任意,能匹配到
                                  execution(* com.test.dao.impl.BookDaoImpl.update(*))
                                  返回值任意,但是update方法必须要有一个参数,无法匹配,要想匹配需要在update接口和实现类添加参数
                                  execution(void com.*.*.*.*.update())
                                  返回值为void,com包下的任意包三层包下的任意类的update方法,匹配到的是实现类,能匹配
                                  execution(void com.*.*.*.update())
                                  返回值为void,com包下的任意两层包下的任意类的update方法,匹配到的是接口,能匹配
                                  execution(void *..update())
                                  返回值为void,方法名是update的任意包下的任意类,能匹配
                                  execution(* *..*(..))
                                  匹配项目中任意类的任意方法,能匹配,但是不建议使用这种方式,影响范围广
                                  execution(* *..u*(..))
                                  匹配项目中任意包任意类下只要以u开头的方法,update方法能满足,能匹配
                                  execution(* *..*e(..))
                                  匹配项目中任意包任意类下只要以e结尾的方法,update和save方法能满足,能匹配
                                  execution(void com..*())
                                  返回值为void,com包下的任意包任意类任意方法,能匹配,*代表的是方法
                                  execution(* com.test.*.*Service.find*(..))
                                  将项目中所有业务层方法的以find开头的方法匹配
                                  execution(* com.test.*.*Service.save*(..))
                                  将项目中所有业务层方法的以save开头的方法匹配
                                  

                                  后面两种更符合我们平常切入点表达式的编写规则。

                                  1.1.3 书写技巧

                                  对于切入点表达式的编写其实是很灵活的,那么在编写的时候,有没有什么好的技巧让我们用用:

                                  • 所有代码按照标准规范开发,否则以下技巧全部失效。
                                  • 描述切入点通常描述接口,而不描述实现类,如果描述到实现类,就出现紧耦合了。
                                  • 访问控制修饰符针对接口开发均采用public描述(可省略访问控制修饰符描述)。
                                  • 返回值类型对于增删改类使用精准类型加速匹配,对于查询类使用*通配快速描述。
                                  • 包名书写尽量不使用…匹配,效率过低,常用*做单个包描述匹配,或精准匹配。
                                  • 接口名/类名书写名称与模块相关的采用*匹配,例如UserService书写成*Service,绑定业务层接口名。
                                  • 方法名书写以动词进行精准匹配,名词采用匹配,例如getById书写成getBy,selectAll书写成selectAll。
                                  • 参数规则较为复杂,根据业务方法灵活调整。
                                  • 通常不使用异常作为匹配规则。

                                    1.2 AOP通知类型

                                    1.2.1 类型介绍

                                    我们先来回顾下AOP通知:

                                    • AOP通知描述了抽取的共性功能,根据共性功能抽取的位置不同,最终运行代码时要将其加入到合理的位置

                                      通知具体要添加到切入点的哪里?

                                      共提供了5种通知类型:

                                      • 前置通知
                                      • 后置通知
                                      • 环绕通知(重点)
                                      • 返回后通知(了解)
                                      • 抛出异常后通知(了解)

                                        为了更好的理解这几种通知类型,我们来看一张图

                                        在这里插入图片描述

                                        (1)前置通知,追加功能到方法执行前,类似于在代码1或者代码2添加内容

                                        (2)后置通知,追加功能到方法执行后,不管方法执行的过程中有没有抛出异常都会执行,类似于在代码5添加内容

                                        (3)返回后通知,追加功能到方法执行后,只有方法正常执行结束后才进行,类似于在代码3添加内容,如果方法执行抛出异常,返回后通知将不会被添加

                                        (4)抛出异常后通知,追加功能到方法抛出异常后,只有方法执行出异常才进行,类似于在代码4添加内容,只有方法抛出异常后才会被添加

                                        (5)环绕通知,环绕通知功能比较强大,它可以追加功能到方法执行的前后,这也是比较常用的方式,它可以实现其他四种通知类型的功能,具体是如何实现的,需要我们往下学习。

                                        1.2.2 环境准备

                                        • 创建一个Maven项目

                                        • pom.xml添加Spring依赖

                                          
                                              
                                                  org.springframework
                                                  spring-context
                                                  5.2.10.RELEASE
                                              
                                              
                                                org.aspectj
                                                aspectjweaver
                                                1.9.4
                                              
                                          
                                          
                                        • 添加BookDao和BookDaoImpl类

                                          public interface BookDao {
                                              public void update();
                                              public int select();
                                          }
                                          @Repository
                                          public class BookDaoImpl implements BookDao {
                                              public void update(){
                                                  System.out.println("book dao update ...");
                                              }
                                              public int select() {
                                                  System.out.println("book dao select is running ...");
                                                  return 100;
                                              }
                                          }
                                          
                                        • 创建Spring的配置类

                                          @Configuration
                                          @ComponentScan("com.test")
                                          @EnableAspectJAutoProxy
                                          public class SpringConfig {
                                          }
                                          
                                        • 创建通知类

                                          @Component
                                          @Aspect
                                          public class MyAdvice {
                                              @Pointcut("execution(void com.test.dao.BookDao.update())")
                                              private void pt(){}
                                              public void before() {
                                                  System.out.println("before advice ...");
                                              }
                                              public void after() {
                                                  System.out.println("after advice ...");
                                              }
                                              public void around(){
                                                  System.out.println("around before advice ...");
                                                  System.out.println("around after advice ...");
                                              }
                                              public void afterReturning() {
                                                  System.out.println("afterReturning advice ...");
                                              }
                                              
                                              public void afterThrowing() {
                                                  System.out.println("afterThrowing advice ...");
                                              }
                                          }
                                          
                                        • 编写App运行类

                                          public class App {
                                              public static void main(String[] args) {
                                                  ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
                                                  BookDao bookDao = ctx.getBean(BookDao.class);
                                                  bookDao.update();
                                              }
                                          }
                                          

                                          最终创建好的项目结构如下:

                                          在这里插入图片描述

                                          1.2.3 通知类型的使用

                                          前置通知

                                          修改MyAdvice,在before方法上添加@Before注解

                                          @Component
                                          @Aspect
                                          public class MyAdvice {
                                              @Pointcut("execution(void com.test.dao.BookDao.update())")
                                              private void pt(){}
                                              
                                              @Before("pt()")
                                              //此处也可以写成 @Before("MyAdvice.pt()"),不建议
                                              public void before() {
                                                  System.out.println("before advice ...");
                                              }
                                          }
                                          

                                          后置通知
                                          @Component
                                          @Aspect
                                          public class MyAdvice {
                                              @Pointcut("execution(void com.test.dao.BookDao.update())")
                                              private void pt(){}
                                              
                                              @Before("pt()")
                                              public void before() {
                                                  System.out.println("before advice ...");
                                              }
                                              @After("pt()")
                                              public void after() {
                                                  System.out.println("after advice ...");
                                              }
                                          }
                                          

                                          环绕通知
                                          基本使用
                                          @Component
                                          @Aspect
                                          public class MyAdvice {
                                              @Pointcut("execution(void com.test.dao.BookDao.update())")
                                              private void pt(){}
                                              
                                              @Around("pt()")
                                              public void around(){
                                                  System.out.println("around before advice ...");
                                                  System.out.println("around after advice ...");
                                              }
                                          }
                                          

                                          在这里插入图片描述

                                          运行结果中,通知的内容打印出来,但是原始方法的内容却没有被执行。

                                          因为环绕通知需要在原始方法的前后进行增强,所以环绕通知就必须要能对原始操作进行调用,具体如何实现?

                                          @Component
                                          @Aspect
                                          public class MyAdvice {
                                              @Pointcut("execution(void com.test.dao.BookDao.update())")
                                              private void pt(){}
                                              
                                              @Around("pt()")
                                              public void around(ProceedingJoinPoint pjp) throws Throwable{
                                                  System.out.println("around before advice ...");
                                                  //表示对原始操作的调用
                                                  pjp.proceed();
                                                  System.out.println("around after advice ...");
                                              }
                                          }
                                          

                                          说明:proceed()为什么要抛出异常?

                                          原因很简单,看下源码就知道了

                                          在这里插入图片描述

                                          再次运行,程序可以看到原始方法已经被执行了

                                          在这里插入图片描述

                                          注意事项

                                          (1)原始方法有返回值的处理

                                          • 修改MyAdvice,对BookDao中的select方法添加环绕通知,
                                            @Component
                                            @Aspect
                                            public class MyAdvice {
                                                @Pointcut("execution(void com.test.dao.BookDao.update())")
                                                private void pt(){}
                                                
                                                @Pointcut("execution(int com.test.dao.BookDao.select())")
                                                private void pt2(){}
                                                
                                                @Around("pt2()")
                                                public void aroundSelect(ProceedingJoinPoint pjp) throws Throwable {
                                                    System.out.println("around before advice ...");
                                                    //表示对原始操作的调用
                                                    pjp.proceed();
                                                    System.out.println("around after advice ...");
                                                }
                                            }
                                            
                                            • 修改App类,调用select方法
                                              public class App {
                                                  public static void main(String[] args) {
                                                      ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
                                                      BookDao bookDao = ctx.getBean(BookDao.class);
                                                      int num = bookDao.select();
                                                      System.out.println(num);
                                                  }
                                              }
                                              

                                              运行后会报错,错误内容为:

                                              Exception in thread “main” org.springframework.aop.AopInvocationException: Null return value from advice does not match primitive return type for: public abstract int com.test.dao.BookDao.select()

                                              at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:226)

                                              at com.sun.proxy.$Proxy19.select(Unknown Source)

                                              at com.test.App.main(App.java:12)

                                              错误大概的意思是:空的返回不匹配原始方法的int返回

                                              • void就是返回Null
                                              • 原始方法就是BookDao下的select方法

                                                所以如果我们使用环绕通知的话,要根据原始方法的返回值来设置环绕通知的返回值,具体解决方案为:

                                                @Component
                                                @Aspect
                                                public class MyAdvice {
                                                    @Pointcut("execution(void com.test.dao.BookDao.update())")
                                                    private void pt(){}
                                                    
                                                    @Pointcut("execution(int com.test.dao.BookDao.select())")
                                                    private void pt2(){}
                                                    
                                                    @Around("pt2()")
                                                    public Object aroundSelect(ProceedingJoinPoint pjp) throws Throwable {
                                                        System.out.println("around before advice ...");
                                                        //表示对原始操作的调用
                                                        Object ret = pjp.proceed();
                                                        System.out.println("around after advice ...");
                                                        return ret;
                                                    }
                                                }
                                                

                                                说明:

                                                ​ 为什么返回的是Object而不是int的主要原因是Object类型更通用。

                                                ​ 在环绕通知中是可以对原始方法返回值就行修改的。

                                                返回后通知
                                                @Component
                                                @Aspect
                                                public class MyAdvice {
                                                    @Pointcut("execution(void com.test.dao.BookDao.update())")
                                                    private void pt(){}
                                                    
                                                    @Pointcut("execution(int com.test.dao.BookDao.select())")
                                                    private void pt2(){}
                                                    
                                                    @AfterReturning("pt2()")
                                                    public void afterReturning() {
                                                        System.out.println("afterReturning advice ...");
                                                    }
                                                }
                                                

                                                在这里插入图片描述

                                                注意:返回后通知是需要在原始方法select正常执行后才会被执行,如果select()方法执行的过程中出现了异常,那么返回后通知是不会被执行。后置通知是不管原始方法有没有抛出异常都会被执行。

                                                异常后通知
                                                @Component
                                                @Aspect
                                                public class MyAdvice {
                                                    @Pointcut("execution(void com.test.dao.BookDao.update())")
                                                    private void pt(){}
                                                    
                                                    @Pointcut("execution(int com.test.dao.BookDao.select())")
                                                    private void pt2(){}
                                                    
                                                    @AfterReturning("pt2()")
                                                    public void afterThrowing() {
                                                        System.out.println("afterThrowing advice ...");
                                                    }
                                                }
                                                

                                                在这里插入图片描述

                                                注意:异常后通知是需要原始方法抛出异常,可以在select()方法中添加一行代码int i = 1/0即可。如果没有抛异常,异常后通知将不会被执行。

                                                学习完这5种通知类型,我们来思考下环绕通知是如何实现其他通知类型的功能的?

                                                因为环绕通知是可以控制原始方法执行的,所以我们把增强的代码写在调用原始方法的不同位置就可以实现不同的通知类型的功能,如:

                                                在这里插入图片描述

                                                通知类型总结
                                                知识点1:@After
                                                名称@After
                                                类型方法注解
                                                位置通知方法定义上方
                                                作用设置当前通知方法与切入点之间的绑定关系,当前通知方法在原始切入点方法后运行
                                                知识点2:@AfterReturning
                                                名称@AfterReturning
                                                类型方法注解
                                                位置通知方法定义上方
                                                作用设置当前通知方法与切入点之间绑定关系,当前通知方法在原始切入点方法正常执行完毕后执行
                                                知识点3:@AfterThrowing
                                                名称@AfterThrowing
                                                类型方法注解
                                                位置通知方法定义上方
                                                作用设置当前通知方法与切入点之间绑定关系,当前通知方法在原始切入点方法运行抛出异常后执行
                                                知识点4:@Around
                                                名称@Around
                                                类型方法注解
                                                位置通知方法定义上方
                                                作用设置当前通知方法与切入点之间的绑定关系,当前通知方法在原始切入点方法前后运行

                                                环绕通知注意事项

                                                1. 环绕通知必须依赖形参ProceedingJoinPoint才能实现对原始方法的调用,进而实现原始方法调用前后同时添加通知
                                                2. 通知中如果未使用ProceedingJoinPoint对原始方法进行调用将跳过原始方法的执行
                                                3. 对原始方法的调用可以不接收返回值,通知方法设置成void即可,如果接收返回值,最好设定为Object类型
                                                4. 原始方法的返回值如果是void类型,通知方法的返回值类型可以设置成void,也可以设置成Object
                                                5. 由于无法预知原始方法运行后是否会抛出异常,因此环绕通知方法必须要处理Throwable异常

                                                介绍完这么多种通知类型,具体该选哪一种呢?

                                                1.3 AOP通知获取数据

                                                目前我们写AOP仅仅是在原始方法前后追加一些操作,接下来我们要说说AOP中数据相关的内容,我们将从获取参数、获取返回值和获取异常三个方面来研究切入点的相关信息。

                                                前面我们介绍通知类型的时候总共讲了五种,那么对于这五种类型都会有参数,返回值和异常吗?

                                                我们先来一个个分析下:

                                                • 获取切入点方法的参数,所有的通知类型都可以获取参数
                                                  • JoinPoint:适用于前置、后置、返回后、抛出异常后通知
                                                  • ProceedingJoinPoint:适用于环绕通知
                                                  • 获取切入点方法返回值,前置和抛出异常后通知是没有返回值,后置通知可有可无,所以不做研究
                                                    • 返回后通知
                                                    • 环绕通知
                                                    • 获取切入点方法运行异常信息,前置和返回后通知是不会有,后置通知可有可无,所以不做研究
                                                      • 抛出异常后通知
                                                      • 环绕通知

                                                        1.3.1 环境准备

                                                        • 创建一个Maven项目

                                                        • pom.xml添加Spring依赖

                                                        • 添加BookDao和BookDaoImpl类

                                                          public interface BookDao {
                                                              public String findName(int id);
                                                          }
                                                          @Repository
                                                          public class BookDaoImpl implements BookDao {
                                                              public String findName(int id) {
                                                                  System.out.println("id:"+id);
                                                                  return "itcast";
                                                              }
                                                          }
                                                          
                                                        • 创建Spring的配置类

                                                          @Configuration
                                                          @ComponentScan("com.test")
                                                          @EnableAspectJAutoProxy
                                                          public class SpringConfig {
                                                          }
                                                          
                                                        • 编写通知类

                                                          @Component
                                                          @Aspect
                                                          public class MyAdvice {
                                                              @Pointcut("execution(* com.test.dao.BookDao.findName(..))")
                                                              private void pt(){}
                                                              @Before("pt()")
                                                              public void before() {
                                                                  System.out.println("before advice ..." );
                                                              }
                                                              @After("pt()")
                                                              public void after() {
                                                                  System.out.println("after advice ...");
                                                              }
                                                              @Around("pt()")
                                                              public Object around() throws Throwable{
                                                                  Object ret = pjp.proceed();
                                                                  return ret;
                                                              }
                                                              @AfterReturning("pt()")
                                                              public void afterReturning() {
                                                                  System.out.println("afterReturning advice ...");
                                                              }
                                                              @AfterThrowing("pt()")
                                                              public void afterThrowing() {
                                                                  System.out.println("afterThrowing advice ...");
                                                              }
                                                          }
                                                          
                                                        • 编写App运行类

                                                          public class App {
                                                              public static void main(String[] args) {
                                                                  ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
                                                                  BookDao bookDao = ctx.getBean(BookDao.class);
                                                                  String name = bookDao.findName(100);
                                                                  System.out.println(name);
                                                              }
                                                          }
                                                          

                                                          最终创建好的项目结构如下:

                                                          在这里插入图片描述

                                                          1.3.2 获取参数

                                                          非环绕通知获取方式

                                                          在方法上添加JoinPoint,通过JoinPoint来获取参数

                                                          @Component
                                                          @Aspect
                                                          public class MyAdvice {
                                                              @Pointcut("execution(* com.test.dao.BookDao.findName(..))")
                                                              private void pt(){}
                                                              @Before("pt()")
                                                              public void before(JoinPoint jp) 
                                                                  Object[] args = jp.getArgs();
                                                                  System.out.println(Arrays.toString(args));
                                                                  System.out.println("before advice ..." );
                                                              }
                                                          	//...其他的略
                                                          }
                                                          

                                                          运行App类,可以获取如下内容,说明参数100已经被获取

                                                          在这里插入图片描述

                                                          思考:方法的参数只有一个,为什么获取的是一个数组?

                                                          因为参数的个数是不固定的,所以使用数组更通配些。

                                                          如果将参数改成两个会是什么效果呢?

                                                          (1)修改BookDao接口和BookDaoImpl实现类

                                                          public interface BookDao {
                                                              public String findName(int id,String password);
                                                          }
                                                          @Repository
                                                          public class BookDaoImpl implements BookDao {
                                                              public String findName(int id,String password) {
                                                                  System.out.println("id:"+id);
                                                                  return "itcast";
                                                              }
                                                          }
                                                          

                                                          (2)修改App类,调用方法传入多个参数

                                                          public class App {
                                                              public static void main(String[] args) {
                                                                  ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
                                                                  BookDao bookDao = ctx.getBean(BookDao.class);
                                                                  String name = bookDao.findName(100,"test");
                                                                  System.out.println(name);
                                                              }
                                                          }
                                                          

                                                          (3)运行App,查看结果,说明两个参数都已经被获取到

                                                          在这里插入图片描述

                                                          说明:

                                                          使用JoinPoint的方式获取参数适用于前置、后置、返回后、抛出异常后通知。

                                                          环绕通知获取方式

                                                          环绕通知使用的是ProceedingJoinPoint,因为ProceedingJoinPoint是JoinPoint类的子类,所以对于ProceedingJoinPoint类中应该也会有对应的getArgs()方法,我们去验证下:

                                                          @Component
                                                          @Aspect
                                                          public class MyAdvice {
                                                              @Pointcut("execution(* com.test.dao.BookDao.findName(..))")
                                                              private void pt(){}
                                                              @Around("pt()")
                                                              public Object around(ProceedingJoinPoint pjp)throws Throwable {
                                                                  Object[] args = pjp.getArgs();
                                                                  System.out.println(Arrays.toString(args));
                                                                  Object ret = pjp.proceed();
                                                                  return ret;
                                                              }
                                                          	//其他的略
                                                          }
                                                          

                                                          运行App后查看运行结果,说明ProceedingJoinPoint也是可以通过getArgs()获取参数

                                                          在这里插入图片描述

                                                          注意:

                                                          • pjp.proceed()方法是有两个构造方法,分别是:

                                                            在这里插入图片描述

                                                            • 调用无参数的proceed,当原始方法有参数,会在调用的过程中自动传入参数

                                                            • 所以调用这两个方法的任意一个都可以完成功能

                                                            • 但是当需要修改原始方法的参数时,就只能采用带有参数的方法,如下:

                                                              @Component
                                                              @Aspect
                                                              public class MyAdvice {
                                                                  @Pointcut("execution(* com.test.dao.BookDao.findName(..))")
                                                                  private void pt(){}
                                                                  @Around("pt()")
                                                                  public Object around(ProceedingJoinPoint pjp) throws Throwable{
                                                                      Object[] args = pjp.getArgs();
                                                                      System.out.println(Arrays.toString(args));
                                                                      args[0] = 666;
                                                                      Object ret = pjp.proceed(args);
                                                                      return ret;
                                                                  }
                                                              	//其他的略
                                                              }
                                                              

                                                              有了这个特性后,我们就可以在环绕通知中对原始方法的参数进行拦截过滤,避免由于参数的问题导致程序无法正确运行,保证代码的健壮性。

                                                              1.3.3 获取返回值

                                                              对于返回值,只有返回后AfterReturing和环绕Around这两个通知类型可以获取,具体如何获取?

                                                              环绕通知获取返回值
                                                              @Component
                                                              @Aspect
                                                              public class MyAdvice {
                                                                  @Pointcut("execution(* com.test.dao.BookDao.findName(..))")
                                                                  private void pt(){}
                                                                  @Around("pt()")
                                                                  public Object around(ProceedingJoinPoint pjp) throws Throwable{
                                                                      Object[] args = pjp.getArgs();
                                                                      System.out.println(Arrays.toString(args));
                                                                      args[0] = 666;
                                                                      Object ret = pjp.proceed(args);
                                                                      return ret;
                                                                  }
                                                              	//其他的略
                                                              }
                                                              

                                                              上述代码中,ret就是方法的返回值,我们是可以直接获取,不但可以获取,如果需要还可以进行修改。

                                                              返回后通知获取返回值
                                                              @Component
                                                              @Aspect
                                                              public class MyAdvice {
                                                                  @Pointcut("execution(* com.test.dao.BookDao.findName(..))")
                                                                  private void pt(){}
                                                                  @AfterReturning(value = "pt()",returning = "ret")
                                                                  public void afterReturning(Object ret) {
                                                                      System.out.println("afterReturning advice ..."+ret);
                                                                  }
                                                              	//其他的略
                                                              }
                                                              

                                                              注意:

                                                              (1)参数名的问题

                                                              在这里插入图片描述

                                                              (2)afterReturning方法参数类型的问题

                                                              参数类型可以写成String,但是为了能匹配更多的参数类型,建议写成Object类型

                                                              (3)afterReturning方法参数的顺序问题

                                                              在这里插入图片描述

                                                              运行App后查看运行结果,说明返回值已经被获取到

                                                              在这里插入图片描述

                                                              1.3.4 获取异常

                                                              对于获取抛出的异常,只有抛出异常后AfterThrowing和环绕Around这两个通知类型可以获取,具体如何获取?

                                                              环绕通知获取异常

                                                              这块比较简单,以前我们是抛出异常,现在只需要将异常捕获,就可以获取到原始方法的异常信息了

                                                              @Component
                                                              @Aspect
                                                              public class MyAdvice {
                                                                  @Pointcut("execution(* com.test.dao.BookDao.findName(..))")
                                                                  private void pt(){}
                                                                  @Around("pt()")
                                                                  public Object around(ProceedingJoinPoint pjp){
                                                                      Object[] args = pjp.getArgs();
                                                                      System.out.println(Arrays.toString(args));
                                                                      args[0] = 666;
                                                                      Object ret = null;
                                                                      try{
                                                                          ret = pjp.proceed(args);
                                                                      }catch(Throwable throwable){
                                                                          t.printStackTrace();
                                                                      }
                                                                      return ret;
                                                                  }
                                                              	//其他的略
                                                              }
                                                              

                                                              在catch方法中就可以获取到异常,至于获取到异常以后该如何处理,这个就和你的业务需求有关了。

                                                              抛出异常后通知获取异常
                                                              @Component
                                                              @Aspect
                                                              public class MyAdvice {
                                                                  @Pointcut("execution(* com.test.dao.BookDao.findName(..))")
                                                                  private void pt(){}
                                                                  @AfterThrowing(value = "pt()",throwing = "t")
                                                                  public void afterThrowing(Throwable t) {
                                                                      System.out.println("afterThrowing advice ..."+t);
                                                                  }
                                                              	//其他的略
                                                              }
                                                              

                                                              如何让原始方法抛出异常,方式有很多,

                                                              @Repository
                                                              public class BookDaoImpl implements BookDao {
                                                                  public String findName(int id,String password) {
                                                                      System.out.println("id:"+id);
                                                                      if(true){
                                                                          throw new NullPointerException();
                                                                      }
                                                                      return "itcast";
                                                                  }
                                                              }
                                                              

                                                              注意:

                                                              在这里插入图片描述

                                                              运行App后,查看控制台,就能看的异常信息被打印到控制台

                                                              在这里插入图片描述

                                                              至此,AOP通知如何获取数据就已经讲解完了,数据中包含参数、返回值、异常(了解)。

                                                              2,AOP总结

                                                              2.1 AOP的核心概念

                                                              • 概念:AOP(Aspect Oriented Programming)面向切面编程,一种编程范式
                                                              • 作用:在不惊动原始设计的基础上为方法进行功能增强
                                                              • 核心概念
                                                                • 代理(Proxy):SpringAOP的核心本质是采用代理模式实现的
                                                                • 连接点(JoinPoint):在SpringAOP中,理解为任意方法的执行
                                                                • 切入点(Pointcut):匹配连接点的式子,也是具有共性功能的方法描述
                                                                • 通知(Advice):若干个方法的共性功能,在切入点处执行,最终体现为一个方法
                                                                • 切面(Aspect):描述通知与切入点的对应关系
                                                                • 目标对象(Target):被代理的原始对象成为目标对象

                                                                  2.2 切入点表达式

                                                                  • 切入点表达式标准格式:动作关键字(访问修饰符 返回值 包名.类/接口名.方法名(参数)异常名)

                                                                    execution(* com.test.service.*Service.*(..))
                                                                    
                                                                  • 切入点表达式描述通配符:

                                                                    • 作用:用于快速描述,范围描述
                                                                    • *:匹配任意符号(常用)
                                                                    • .. :匹配多个连续的任意符号(常用)
                                                                    • +:匹配子类类型
                                                                    • 切入点表达式书写技巧

                                                                      1.按标准规范开发

                                                                      2.查询操作的返回值建议使用*匹配

                                                                      3.减少使用…的形式描述包

                                                                      4.对接口进行描述,使用*表示模块名,例如UserService的匹配描述为*Service

                                                                      5.方法名书写保留动词,例如get,使用*表示名词,例如getById匹配描述为getBy*

                                                                      6.参数根据实际情况灵活调整

                                                                      2.3 五种通知类型

                                                                      • 前置通知
                                                                      • 后置通知
                                                                      • 环绕通知(重点)
                                                                        • 环绕通知依赖形参ProceedingJoinPoint才能实现对原始方法的调用
                                                                        • 环绕通知可以隔离原始方法的调用执行
                                                                        • 环绕通知返回值设置为Object类型
                                                                        • 环绕通知中可以对原始方法调用过程中出现的异常进行处理
                                                                        • 返回后通知
                                                                        • 抛出异常后通知

                                                                          2.4 通知中获取参数

                                                                          • 获取切入点方法的参数,所有的通知类型都可以获取参数
                                                                            • JoinPoint:适用于前置、后置、返回后、抛出异常后通知
                                                                            • ProceedingJoinPoint:适用于环绕通知
                                                                            • 获取切入点方法返回值,前置和抛出异常后通知是没有返回值,后置通知可有可无,所以不做研究
                                                                              • 返回后通知
                                                                              • 环绕通知
                                                                              • 获取切入点方法运行异常信息,前置和返回后通知是不会有,后置通知可有可无,所以不做研究
                                                                                • 抛出异常后通知
                                                                                • 环绕通知

                                                                                  后记

                                                                                  👉👉💕💕美好的一天,到此结束,下次继续努力!欲知后续,请看下回分解,写作不易,感谢大家的支持!! 🌹🌹🌹