五、Spring AOP面向切面编程
5.1 场景设定和问题复现
- 准备AOP项目项目名:spring-aop-annotationpom.xml
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
| <dependencies> <dependency> <groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.6</version>
</dependency>
<dependency> <groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.3.1</version>
</dependency>
<dependency> <groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>6.0.6</version>
<scope>test</scope>
</dependency>
<dependency> <groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<version>2.1.1</version>
</dependency>
</dependencies>
|
- 声明接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
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 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.atguigu.proxy;
public class CalculatorPureImpl implements Calculator { @Override public int add(int i, int j) { int result = i + j; return result; } @Override public int sub(int i, int j) { int result = i - j; return result; } @Override public int mul(int i, int j) { int result = i * j; return result; } @Override public int div(int i, int j) { int result = i / j; 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
| package com.atguigu.proxy;
public class CalculatorLogImpl implements Calculator { @Override public int add(int i, int j) { System.out.println("参数是:" + i + "," + j); int result = i + j; System.out.println("方法内部 result = " + result); return result; } @Override public int sub(int i, int j) { System.out.println("参数是:" + i + "," + j); int result = i - j; System.out.println("方法内部 result = " + result); return result; } @Override public int mul(int i, int j) { System.out.println("参数是:" + i + "," + j); int result = i * j; System.out.println("方法内部 result = " + result); return result; } @Override public int div(int i, int j) { System.out.println("参数是:" + i + "," + j); int result = i / j; System.out.println("方法内部 result = " + result); return result; } }
|
- 代码问题分析
- 代码缺陷
- 对核心业务功能有干扰,导致程序员在开发核心业务功能时分散了精力
- 附加功能代码重复,分散在各个业务功能方法中!冗余,且不方便统一维护!
- 解决思路 核心就是:解耦。我们需要把附加功能从业务功能代码中抽取出来。 将重复的代码统一提取,并且[[动态插入]]到每个业务方法!
- 技术困难解决问题的困难:提取重复附加功能代码到一个类中,可以实现但是如何将代码插入到各个方法中,我们不会,我们需要引用新技术!!!
5.2 解决技术代理模式
- 代理模式
二十三种设计模式中的一种,属于结构型模式。它的作用就是通过提供一个代理类,让我们在调用目标方法的时候,不再是直接对目标方法进行调用,而是通过代理类间接调用。让不属于目标方法核心逻辑的代码从目标方法中剥离出来——解耦。调用目标方法时先调用代理对象的方法,减少对目标方法的调用和打扰,同时让附加功能能够集中在一起也有利于统一维护。
无代理场景:

有代理场景:

生活中的代理:
- 广告商找大明星拍广告需要经过经纪人
- 合作伙伴找大老板谈合作要约见面时间需要经过秘书
- 房产中介是买卖双方的代理
- 太监是大臣和皇上之间的代理
相关术语:
- 代理:将非核心逻辑剥离出来以后,封装这些非核心逻辑的类、对象、方法。(中介)
- 动词:指做代理这个动作,或这项工作
- 名词:扮演代理这个角色的类、对象、方法
- 目标:被代理“套用”了核心逻辑代码的类、对象、方法。(房东)
代理在开发中实现的方式具体有两种:静态代理,[动态代理技术]
- 静态代理主动创建代理类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| public class CalculatorStaticProxy implements Calculator { private Calculator target; public CalculatorStaticProxy(Calculator target) { this.target = target; } @Override public int add(int i, int j) { System.out.println("参数是:" + i + "," + j); int addResult = target.add(i, j); System.out.println("方法内部 result = " + result); return addResult; } ……
|
静态代理确实实现了解耦,但是由于代码都写死了,完全不具备任何的灵活性。就拿日志功能来说,将来其他地方也需要附加日志,那还得再声明更多个静态代理类,那就产生了大量重复的代码,日志功能还是分散的,没有统一管理。
提出进一步的需求:将日志功能集中到一个代理类中,将来有任何日志需求,都通过这一个代理类来实现。这就需要使用动态代理技术了。
- 动态代理
动态代理技术分类
- JDK动态代理:JDK原生的实现方式,需要被代理的目标类必须**实现接口**!他会根据目标类的接口动态生成一个代理对象!代理对象和目标对象有相同的接口!(拜把子)
- cglib:通过继承被代理的目标类实现代理,所以不需要目标类实现接口!(认干爹)
- JDK动态代理技术实现(了解)
- 
代理工程:基于jdk代理技术,生成代理对象
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
| public class ProxyFactory {
private Object target;
public ProxyFactory(Object target) { this.target = target; }
public Object getProxy(){
ClassLoader classLoader = target.getClass().getClassLoader(); Class<?>[] interfaces = target.getClass().getInterfaces(); InvocationHandler invocationHandler = new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object result = null; try { System.out.println("[动态代理][日志] "+method.getName()+",参数:"+ Arrays.toString(args)); result = method.invoke(target, args); System.out.println("[动态代理][日志] "+method.getName()+",结果:"+ result); } catch (Exception e) { e.printStackTrace(); System.out.println("[动态代理][日志] "+method.getName()+",异常:"+e.getMessage()); } finally { System.out.println("[动态代理][日志] "+method.getName()+",方法执行完毕"); } return result; } };
return Proxy.newProxyInstance(classLoader, interfaces, invocationHandler); } }
|
测试代码:
1 2 3 4 5 6 7
| @Test public void testDynamicProxy(){ ProxyFactory factory = new ProxyFactory(new CalculatorLogImpl()); Calculator proxy = (Calculator) factory.getProxy(); proxy.div(1,0); }
|
-
* 
- 代理总结
代理方式可以解决附加功能代码干扰核心代码和不方便统一维护的问题!
他主要是将附加功能代码提取到代理中执行,不干扰目标核心代码!
但是我们也发现,无论使用静态代理和动态代理(jdk,cglib),程序员的工作都比较繁琐!
需要自己编写代理工厂等!
但是,提前剧透,我们在实际开发中,不需要编写代理代码,我们可以使用[Spring AOP]框架,他会简化动态代理的实现!!!
5.3 面向切面编程思维(AOP)
- 面向切面编程思想AOP
AOP:Aspect Oriented Programming面向切面编程
AOP可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善。OOP引入封装、继承、多态等概念来建立一种对象层次结构,用于模拟公共行为的一个集合。不过OOP允许开发者定义纵向的关系,但并不适合定义横向的关系,例如日志功能。日志代码往往横向地散布在所有对象层次中,而与它对应的对象的核心功能毫无关系对于其他类型的代码,如安全性、异常处理和透明的持续性也都是如此,这种散布在各处的无关的代码被称为横切(cross cutting),在OOP设计中,它导致了大量代码的重复,而不利于各个模块的重用。
AOP技术恰恰相反,它利用一种称为”横切”的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装到一个可重用模块,并将其命名为”Aspect”,即切面。所谓”切面”,简单说就是那些与业务无关,却为业务模块所共同调用的逻辑或责任封装起来,便于减少系统的重复代码,降低模块之间的耦合度,并有利于未来的可操作性和可维护性。
使用AOP,可以在不修改原来代码的基础上添加新功能。
- AOP思想主要的应用场景AOP(面向切面编程)是一种编程范式,它通过将通用的横切关注点(如日志、事务、权限控制等)与业务逻辑分离,使得代码更加清晰、简洁、易于维护。AOP可以应用于各种场景,以下是一些常见的AOP应用场景:
- 日志记录:在系统中记录日志是非常重要的,可以使用AOP来实现日志记录的功能,可以在方法执行前、执行后或异常抛出时记录日志。
- 事务处理:在数据库操作中使用事务可以保证数据的一致性,可以使用AOP来实现事务处理的功能,可以在方法开始前开启事务,在方法执行完毕后提交或回滚事务。
- 安全控制:在系统中包含某些需要安全控制的操作,如登录、修改密码、授权等,可以使用AOP来实现安全控制的功能。可以在方法执行前进行权限判断,如果用户没有权限,则抛出异常或转向到错误页面,以防止未经授权的访问。
- 性能监控:在系统运行过程中,有时需要对某些方法的性能进行监控,以找到系统的瓶颈并进行优化。可以使用AOP来实现性能监控的功能,可以在方法执行前记录时间戳,在方法执行完毕后计算方法执行时间并输出到日志中。
- 异常处理:系统中可能出现各种异常情况,如空指针异常、数据库连接异常等,可以使用AOP来实现异常处理的功能,在方法执行过程中,如果出现异常,则进行异常处理(如记录日志、发送邮件等)。
- 缓存控制:在系统中有些数据可以缓存起来以提高访问速度,可以使用AOP来实现缓存控制的功能,可以在方法执行前查询缓存中是否有数据,如果有则返回,否则执行方法并将方法返回值存入缓存中。
- 动态代理:AOP的实现方式之一是通过动态代理,可以代理某个类的所有方法,用于实现各种功能。
综上所述,AOP可以应用于各种场景,它的作用是将通用的横切关注点与业务逻辑分离,使得代码更加清晰、简洁、易于维护。
- AOP术语名词介绍
1-横切关注点
从每个方法中抽取出来的同一类非核心业务。在同一个项目中,我们可以使用多个横切关注点对相关方法进行多个不同方面的增强。
这个概念不是语法层面天然存在的,而是根据附加功能的逻辑上的需要:有十个附加功能,就有十个横切关注点。

AOP把软件系统分为两个部分:核心关注点和横切关注点。业务处理的主要流程是核心关注点,与之关系不大的部分是横切关注点。横切关注点的一个特点是,他们经常发生在核心关注点的多处,而各处基本相似,比如权限认证、日志、事务、异常等。AOP的作用在于分离系统中的各种关注点,将核心关注点和横切关注点分离开来。
2-通知(增强)
每一个横切关注点上要做的事情都需要写一个方法来实现,这样的方法就叫通知方法。
- 前置通知:在被代理的目标方法前执行
- 返回通知:在被代理的目标方法成功结束后执行(寿终正寝)
- 异常通知:在被代理的目标方法异常结束后执行(死于非命)
- 后置通知:在被代理的目标方法最终结束后执行(盖棺定论)
- 环绕通知:使用try…catch…finally结构围绕整个被代理的目标方法,包括上面四种通知对应的所有位置

3-连接点 joinpoint
这也是一个纯逻辑概念,不是语法定义的。
指那些被拦截到的点。在 Spring 中,可以被动态代理拦截目标类的方法


4-切入点 pointcut
定位连接点的方式,或者可以理解成被选中的连接点!
是一个表达式,比如execution(* com.spring.service.impl..(..))。符合条件的每个方法都是一个具体的连接点。
5-切面 aspect
切入点和通知的结合。是一个类。


6-目标 target
被代理的目标对象。
7-代理 proxy
向目标对象应用通知之后创建的代理对象。
8-织入 weave
指把通知应用到目标上,生成代理对象的过程。可以在编译期织入,也可以在运行期织入,Spring采用后者。
5.4 Spring AOP框架介绍和关系梳理
- AOP一种区别于OOP的编程思维,用来完善和解决OOP的非核心代码冗余和不方便统一维护问题!
- 代理技术(动态代理|静态代理)是实现AOP思维编程的具体技术,但是自己使用动态代理实现代码比较繁琐!
- Spring AOP框架,基于AOP编程思维,封装动态代理技术,简化动态代理技术实现的框架!SpringAOP内部帮助我们实现动态代理,我们只需写少量的配置,指定生效范围即可,即可完成面向切面思维编程的实现!
5.5 Spring AOP基于注解方式实现和细节
5.5.1 Spring AOP底层技术组成


- 动态代理(InvocationHandler):JDK原生的实现方式,需要被代理的目标类必须实现接口。因为这个技术要求代理对象和目标对象实现同样的接口(兄弟两个拜把子模式)。
- cglib:通过继承被代理的目标类(认干爹模式)实现代理,所以不需要目标类实现接口。
- AspectJ:早期的AOP实现的框架,SpringAOP借用了AspectJ中的AOP注解。
5.5.2 初步实现
- 加入依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <dependency> <groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>6.0.6</version>
</dependency>
<dependency> <groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>6.0.6</version>
</dependency>
|
- 准备接口
1 2 3 4 5 6 7 8 9 10 11
| 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 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
| package com.atguigu.proxy;
@Component public class CalculatorPureImpl implements Calculator { @Override public int add(int i, int j) { int result = i + j; return result; } @Override public int sub(int i, int j) { int result = i - j; return result; } @Override public int mul(int i, int j) { int result = i * j; return result; } @Override public int div(int i, int j) { int result = i / j; 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
| package com.atguigu.advice;
import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component;
@Aspect
@Component public class LogAspect { @Before(value = "execution(public int com.atguigu.proxy.CalculatorPureImpl.add(int,int))") public void printLogBeforeCore() { System.out.println("[AOP前置通知] 方法开始了"); } @AfterReturning(value = "execution(public int com.atguigu.proxy.CalculatorPureImpl.add(int,int))") public void printLogAfterSuccess() { System.out.println("[AOP返回通知] 方法成功返回了"); } @AfterThrowing(value = "execution(public int com.atguigu.proxy.CalculatorPureImpl.add(int,int))") public void printLogAfterException() { System.out.println("[AOP异常通知] 方法抛异常了"); } @After(value = "execution(public int com.atguigu.proxy.CalculatorPureImpl.add(int,int))") public void printLogFinallyEnd() { System.out.println("[AOP后置通知] 方法最终结束了"); } }
|
- 开启aspectj注解支持
- xml方式
1 2 3 4 5 6 7 8 9 10 11 12 13
| <?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 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.atguigu" /> <aop:aspectj-autoproxy /> </beans>
|
1. 配置类方式
1 2 3 4 5 6 7
| @Configuration @ComponentScan(basePackages = "com.atguigu")
@EnableAspectJAutoProxy public class MyConfig { }
|
6.测试效果
1 2 3 4 5 6 7 8 9 10 11 12 13
| @SpringJUnitConfig(value = {MyConfig.class}) public class AopTest {
@Autowired private Calculator calculator;
@Test public void testCalculator(){ calculator.add(1,1); } }
|
输出结果:
1 2 3 4
| com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit5 com.atguigu.test.AopTest,testCalculator [AOP前置通知] 方法开始了 [AOP返回通知] 方法成功返回了 [AOP后置通知] 方法最终结束了
|
5.5.3 获取通知细节信息
- JointPoint接口
需要获取方法签名、传入的实参等信息时,可以在通知方法声明JoinPoint类型的形参。
- 要点1:JoinPoint 接口通过 getSignature() 方法获取目标方法的签名(方法声明时的完整信息)
- 要点2:通过目标方法签名对象获取方法名
- 要点3:通过 JoinPoint 对象获取外界调用目标方法时传入的实参列表组成的数组
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
|
@Before(value = "execution(public int com.atguigu.aop.api.Calculator.add(int,int))") public void printLogBeforeCore(JoinPoint joinPoint) { Signature signature = joinPoint.getSignature(); String methodName = signature.getName(); System.out.println("methodName = " + methodName); int modifiers = signature.getModifiers(); System.out.println("modifiers = " + modifiers); String declaringTypeName = signature.getDeclaringTypeName(); System.out.println("declaringTypeName = " + declaringTypeName); Object[] args = joinPoint.getArgs(); List<Object> argList = Arrays.asList(args); System.out.println("[AOP前置通知] " + methodName + "方法开始了,参数列表:" + argList); }
|
-
- 方法返回值
在返回通知中,通过 @AfterReturning注解的returning属性获取目标方法的返回值!
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
@AfterReturning( value = "execution(public int com.atguigu.aop.api.Calculator.add(int,int))", returning = "targetMethodReturnValue" ) public void printLogAfterCoreSuccess(JoinPoint joinPoint, Object targetMethodReturnValue) { String methodName = joinPoint.getSignature().getName(); System.out.println("[AOP返回通知] "+methodName+"方法成功结束了,返回值是:" + targetMethodReturnValue); }
|
- 异常对象捕捉
在异常通知中,通过@AfterThrowing注解的throwing属性获取目标方法抛出的异常对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
@AfterThrowing( value = "execution(public int com.atguigu.aop.api.Calculator.add(int,int))", throwing = "targetMethodException" ) public void printLogAfterCoreException(JoinPoint joinPoint, Throwable targetMethodException) { String methodName = joinPoint.getSignature().getName(); System.out.println("[AOP异常通知] "+methodName+"方法抛异常了,异常类型是:" + targetMethodException.getClass().getName()); }
|
5.5.4 切点表达式语法
- 切点表达式作用AOP切点表达式(Pointcut Expression)是一种用于指定切点的语言,它可以通过定义匹配规则,来选择需要被切入的目标对象。

- 切点表达式语法
切点表达式总结

- 语法细节
- 第一位:execution( ) 固定开头
- 第二位:方法访问修饰符
1
| public private 直接描述对应修饰符即可
|
- 第三位:方法返回值
1 2
| int String void 直接描述返回值类型
|
注意:
特殊情况 不考虑 访问修饰符和返回值
execution(* * ) 这是错误语法
execution( *) == 你只要考虑返回值 或者 不考虑访问修饰符 相当于全部不考虑了
- 第四位:指定包的地址
1 2 3 4 5
| 固定的包: com.atguigu.api | service | dao 单层的任意命名: com.atguigu.* = com.atguigu.api com.atguigu.dao * = 任意一层的任意命名 任意层任意命名: com.. = com.atguigu.api.erdaye com.a.a.a.a.a.a.a ..任意层,任意命名 用在包上! 注意: ..不能用作包开头 public int .. 错误语法 com.. 找到任何包下: *..
|
- 第五位:指定类名称
1 2 3 4 5
| 固定名称: UserService 任意类名: * 部分任意: com..service.impl.*Impl 任意包任意类: *..*
|
- 第六位:指定方法名称
1 2
| 语法和类名一致 任意访问修饰符,任意类的任意方法: * *..*.*
|
- 第七位:方法参数
1 2 3 4 5 6 7 8
| 第七位: 方法的参数描述 具体值: (String,int) != (int,String) 没有参数 () 模糊值: 任意参数 有 或者 没有 (..) ..任意参数的意识 部分具体和模糊: 第一个参数是字符串的方法 (String..) 最后一个参数是字符串 (..String) 字符串开头,int结尾 (String..int) 包含int类型(..int..)
|
- 切点表达式案例
1 2 3 4 5 6 7 8 9 10 11
| 1.查询某包某类下,访问修饰符是公有,返回值是int的全部方法 public int xx.xx.jj.*(..) 2.查询某包下类中第一个参数是String的方法 * xx.xx.jj.*(String..) 3.查询全部包下,无参数的方法! * *..*.*() 4.查询com包下,以int参数类型结尾的方法 * com..*.*(..int) 5.查询指定包下,Service开头类的私有返回值int的无参数方法 private int xx.xx.Service*.*()
|
5.5.5 重用(提取)切点表达式
- 重用切点表达式优点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
@Before(value = "execution(public int com.atguigu.proxy.CalculatorPureImpl.add(int,int))") public void printLogBeforeCore() { System.out.println("[AOP前置通知] 方法开始了"); }
@AfterReturning(value = "execution(public int com.atguigu.proxy.CalculatorPureImpl.add(int,int))") public void printLogAfterSuccess() { System.out.println("[AOP返回通知] 方法成功返回了"); }
@AfterThrowing(value = "execution(public int com.atguigu.proxy.CalculatorPureImpl.add(int,int))") public void printLogAfterException() { System.out.println("[AOP异常通知] 方法抛异常了"); }
@After(value = "execution(public int com.atguigu.proxy.CalculatorPureImpl.add(int,int))") public void printLogFinallyEnd() { System.out.println("[AOP后置通知] 方法最终结束了"); }
|
上面案例,是我们之前编写切点表达式的方式,发现, 所有增强方法的切点表达式相同!
出现了冗余,如果需要切换也不方便统一维护!
我们可以将切点提取,在增强上进行引用即可!
- 同一类内部引用提取
1 2 3
| @Pointcut("execution(public int com.atguigu.aop.api.Calculator.add(int,int)))") public void declarPointCut() {}
|
注意:提取切点注解使用@Pointcut(切点表达式) , 需要添加到一个无参数无返回值方法上即可!
引用
1 2
| @Before(value = "declarPointCut()") public void printLogBeforeCoreOperation(JoinPoint joinPoint) {
|
- 不同类中引用
不同类在引用切点,只需要添加类的全限定符+方法名即可!
1 2
| @Before(value = "com.atguigu.spring.aop.aspect.LogAspect.declarPointCut()") public Object roundAdvice(ProceedingJoinPoint joinPoint) {
|
- 切点统一管理
建议:将切点表达式统一存储到一个类中进行集中管理和维护!
1 2 3 4 5 6 7 8 9 10 11 12
| @Component public class AtguiguPointCut { @Pointcut(value = "execution(public int *..Calculator.sub(int,int))") public void atguiguGlobalPointCut(){} @Pointcut(value = "execution(public int *..Calculator.add(int,int))") public void atguiguSecondPointCut(){} @Pointcut(value = "execution(* *..*Service.*(..))") public void transactionPointCut(){} }
|
5.5.6 环绕通知
环绕通知对应整个 try…catch…finally 结构,包括前面四种通知的所有功能。
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
| @Around(value = "com.atguigu.aop.aspect.AtguiguPointCut.transactionPointCut()") public Object manageTransaction( // 通过在通知方法形参位置声明ProceedingJoinPoint类型的形参, // Spring会将这个类型的对象传给我们 ProceedingJoinPoint joinPoint) { Object[] args = joinPoint.getArgs(); Signature signature = joinPoint.getSignature(); String methodName = signature.getName(); Object targetMethodReturnValue = null; try { log.debug("[AOP 环绕通知] 开启事务,方法名:" + methodName + ",参数列表:" + Arrays.asList(args)); targetMethodReturnValue = joinPoint.proceed(args); log.debug("[AOP 环绕通知] 提交事务,方法名:" + methodName + ",方法返回值:" + targetMethodReturnValue); }catch (Throwable e){ log.debug("[AOP 环绕通知] 回滚事务,方法名:" + methodName + ",异常:" + e.getClass().getName()); }finally { log.debug("[AOP 环绕通知] 释放数据库连接,方法名:" + methodName); } return targetMethodReturnValue; }
|
5.5.7 切面优先级设置
相同目标方法上同时存在多个切面时,切面的优先级控制切面的内外嵌套顺序。
使用 @Order 注解可以控制切面的优先级:
- @Order(较小的数):优先级高
- @Order(较大的数):优先级低


实际意义
实际开发时,如果有多个切面嵌套的情况,要慎重考虑。例如:如果事务切面优先级高,那么在缓存中命中数据的情况下,事务切面的操作都浪费了。


此时应该将缓存切面的优先级提高,在事务操作之前先检查缓存中是否存在目标数据。
5.5.8 CGLib动态代理生效
在目标类没有实现任何接口的情况下,Spring会自动使用cglib技术实现代理。为了证明这一点,我们做下面的测试:
1 2 3 4 5 6 7
| @Service public class EmployeeService { public void getEmpList() { System.out.print("方法内部 com.atguigu.aop.imp.EmployeeService.getEmpList"); } }
|
测试:
1 2 3 4 5 6 7
| @Autowired private EmployeeService employeeService;
@Test public void testNoInterfaceProxy() { employeeService.getEmpList(); }
|
没有接口:

有接口:

使用总结:
a. 如果目标类有接口,选择使用jdk动态代理
b. 如果目标类没有接口,选择cglib动态代理
c. 如果有接口,接口接值
d. 如果没有接口,类进行接值
5.5.9 注解实现小结


5.6 Spring AOP基于XML方式实现(了解)
- 准备工作
加入依赖
和基于注解的 AOP 时一样。
准备代码
把测试基于注解功能时的Java类复制到新module中,去除所有注解。
- 配置Spring配置文件
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
| <bean id="calculatorPure" class="com.atguigu.aop.imp.CalculatorPureImpl"/>
<bean id="logAspect" class="com.atguigu.aop.aspect.LogAspect"/>
<aop:config> <aop:pointcut id="logPointCut" expression="execution(* *..*.*(..))"/> <aop:aspect ref="logAspect"> <aop:before method="printLogBeforeCore" pointcut-ref="logPointCut"/> <aop:after-returning method="printLogAfterCoreSuccess" pointcut-ref="logPointCut" returning="targetMethodReturnValue"/> <aop:after-throwing method="printLogAfterCoreException" pointcut-ref="logPointCut" throwing="targetMethodException"/> <aop:after method="printLogCoreFinallyEnd" pointcut-ref="logPointCut"/> </aop:aspect>
</aop:config>
|
- 测试
1 2 3 4 5 6 7 8 9 10 11 12
| @SpringJUnitConfig(locations = "classpath:spring-aop.xml") public class AopTest {
@Autowired private Calculator calculator;
@Test public void testCalculator(){ System.out.println(calculator); calculator.add(1,1); } }
|
5.7 Spring AOP对获取Bean的影响理解
5.7.1 根据类型装配 bean
- 情景一
- bean 对应的类没有实现任何接口
- 根据 bean 本身的类型获取 bean
- 测试:IOC容器中同类型的 bean 只有一个正常获取到 IOC 容器中的那个 bean 对象
- 测试:IOC 容器中同类型的 bean 有多个会抛出 NoUniqueBeanDefinitionException 异常,表示 IOC 容器中这个类型的 bean 有多个
- 情景二
- bean 对应的类实现了接口,这个接口也只有这一个实现类
- 测试:根据接口类型获取 bean
- 测试:根据类获取 bean
- 结论:上面两种情况其实都能够正常获取到 bean,而且是同一个对象
- 情景三
- 声明一个接口
- 接口有多个实现类
- 接口所有实现类都放入 IOC 容器
- 测试:根据接口类型获取 bean会抛出 NoUniqueBeanDefinitionException 异常,表示 IOC 容器中这个类型的 bean 有多个
- 测试:根据类获取bean正常
- 情景四
- 声明一个接口
- 接口有一个实现类
- 创建一个切面类,对上面接口的实现类应用通知
- 测试:根据接口类型获取bean正常
- 测试:根据类获取bean无法获取
原因分析:
- 应用了切面后,真正放在IOC容器中的是代理类的对象
- 目标类并没有被放到IOC容器中,所以根据目标类的类型从IOC容器中是找不到的

- 情景五

* 测试:根据类获取 bean,能获取到

debug查看实际类型:
5.7.2 使用总结
对实现了接口的类应用切面


对没实现接口的类应用切面new


如果使用AOP技术,目标类有接口,必须使用接口类型接收IoC容器中代理组件!