Spring AOP (面向切面编程)
Spring AOP (面向切面编程)
1、AOP 前奏
package com.spring.aop.helloworld;
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
public int add(int i, int j) {
System.out.println("The method add begins with[" + i + "," + j + "]");
int result = i + j;
System.out.println("The method add ends with " + result);
return result;
}
public int sub(int i, int j) {
System.out.println("The method sub begins with[" + i + "," + j + "]");
int result = i - j;
System.out.println("The method sub ends with " + result);
return result;
}
public int mul(int i, int j) {
System.out.println("The method mul begins with[" + i + "," + j + "]");
int result = i * j;
System.out.println("The method mul ends with " + result);
return result;
}
public int div(int i, int j) {
System.out.println("The method div begins with[" + i + "," + j + "]");
int result = i / j;
System.out.println("The method div ends with " + result);
return result;
}
}
问题:
- 代码混乱:越来越多的非业务需求(日志和验证等)加入后,原有的业务方法急剧膨胀。每个方法在处理核心逻辑的同时还必须兼顾其他多个关注点。
- 代码分散:以日志需求为例,只是为了满足这个单一需求,就不得不在多个模块(方法)里多次重复相同的日志代码。如果日志需求发生变化,必须修改所有模块。
2、使用动态代理解决上述问题
代理设计模式的原理:使用一个代理将对象包装起来,然后用该代理对象取代原始对象。任何对原始对象的调用都要通过代理。代理对象决定是否以及何时将方法调用转到原始的对象上。
public class ArithmeticCalculatorLoggingProxy { //要代理的对象 private ArithmeticCalculator target; public ArithmeticCalculatorLoggingProxy(ArithmeticCalculator target) { this.target = target; } public ArithmeticCalculator getLoggingProxy() { ArithmeticCalculator proxy = null; //代理对象由哪一个类加载器负责加载 ClassLoader loader = target.getClass().getClassLoader(); //代理对象的类型,即其中有哪些方法 Class [] interfaces = new Class[]{ArithmeticCalculator.class}; //当调用代理对象其中的方法时,该执行的代码 InvocationHandler h = new InvocationHandler() { /** * @param proxy:正在返回的那个代理对象,一般情况下,在 invoke 方法中都不使用该对象 * @param method:正在被调用的方法 * @param args:调用方法时传入的参数 * @return * @throws Throwable */ @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { String methodName = method.getName(); //日志 System.out.println("The method " + methodName + " begins with " + Arrays.asList(args)); //执行方法 Object result = method.invoke(target, args); //日志 System.out.println("The method " + methodName + " ends with " + result); return result; } }; proxy = (ArithmeticCalculator) Proxy.newProxyInstance(loader, interfaces, h); return proxy; } } public class Main { public static void main(String[] args) { //ArithmeticCalculator arithmeticCalculator = new ArithmeticCalculatorLoggingImpl(); // arithmeticCalculator = new ArithmeticCalculatorImpl(); ArithmeticCalculator target = new ArithmeticCalculatorImpl(); ArithmeticCalculator proxy = new ArithmeticCalculatorLoggingProxy(target).getLoggingProxy(); int result = proxy.add(1, 2); System.out.println("-->" + result); result = proxy.div(4, 2); System.out.println("-->" + result); } }
3、AOP 简介
- AOP (Aspect-OrientedProgramming,面向切面编程):是一种新的方法论,是对传统 OOP(Object-OrientedProgramming,面向对象编程)的补充。
- AOP 的主要编程对象是切面(aspect),而切面模块化横切关注点。
- 在应用 AOP 编程时,仍然需要定义公共功能,但可以明确的定义这个功能在哪里,以什么方式应用,并且不必修改受影响的类。这样一来横切关注点就被模块化到特殊的对象(切面)里。
AOP 的好处:
- 每个事物逻辑位于一个位置,代码不分散,便于维护和升级。
- 业务模块更简洁,只包含核心业务代码。
4、AOP 术语
- 切面(Aspect):横切关注点(跨越应用程序多个模块的功能)被模块化的特殊对象
- 通知(Advice):切面必须要完成的工作
- 目标(Target):被通知的对象
- 代理(Proxy):向目标对象应用通知之后创建的对象
- 连接点(Joinpoint):程序执行的某个特定位置:如类某个方法调用前、调用后、方法抛出异常后等。连接点由两个信息确定:方法表示的程序执行点;相对点表示的方位。例如 ArithmeticCalculator#add() 方法执行前的连接点,执行点为 ArithmeticCalculator#add();方位为该方法执行前的位置。
- 切点(pointcut):每个类都拥有多个连接点;例如:ArithmeticCalculator 的所有方法实际上都是连接点,即连接点是程序类中客观存在的事务。AOP 通过切点定位到特定的连接点。类比:连接点相当于数据库中的记录,切点相当于查询条件。切点和连接点不是一对一的关系,一个切点匹配多个连接点,切点通过 org.springframework.aop.Pointcut 接口进行描述,它使用类和方法作为连接点的查询条件。
5、在 Spring 中启用 AspectJ 注解支持
- 要在 Spring 应用中使用 Aspect 注解,必须在 classpath 下包含 AspectJ 类库:aopalliance.jar、aspectj.weaver,jar 和 spring-aspects.jar
- 将 aopSchema 添加到 <beans> 根元素中。
- 要在 Spring IOC 容器中启用 AspectJ注解支持,只要在 Bean 配置文件中定义一个空的 XML 元素 <aop:aspectj-autoproxy>
- 当 Spring IOC 容器侦测到 Bean 配置文件中的 <aop:aspectj-autoproxy> 元素时,会自动为与 AspectJ 切面匹配的 Bean 创建代理。
6、使用 SpringAOP 步骤
加入 jar 包:
<dependencies> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.4</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.3</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.0.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>5.0.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.0.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.0.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>5.0.5.RELEASE</version> </dependency> </dependencies>
在配置文件中加入 aop 的命名空间
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
基于注解的方式
在配置文件中加入如下配置
<!-- 使 AspectJ 注解起作用:自动为匹配的类生成代理对象--> <aop:aspectj-autoproxy/>
把横切关注点的代码抽象到切面的类中。
- 切面首先是一个 IOC 中的 bean ,即加入 @Component 注解
- 切面还需要加入 @Aspect 注解
//把这个类声明为一个切面:需要把该类放入到 IOC 容器中、再声明为一个切面 @Aspect @Component public class LoggingAspect { //声明该方法是一个前置通知:在目标方法开始之前执行 @Before("execution(public int com.spring.aop.impl.ArithmeticCalculator.*(int, int))") public void beforeMethod(JoinPoint joinPoint) { String methodName = joinPoint.getSignature().getName(); List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("The method " + methodName + " begins with " + args); }
在类中声明各种通知
AspectJ 支持 5 种类型的通知注解:
- @Before:前置通知,在方法执行之前执行
- @After:后置通知,在方法执行之后执行
@AfterRunning:返回通知,在方法返回结果之后执行
@AfterThrowing:异常通知,在方法抛出异常之后执行
- @Around:环绕通知,围绕着方法执行
可以在通知方法中声明一个类型为 JoinPoint 的参数,然后就能访问链接细节,如方法名称和参数值。
//声明该方法是一个前置通知:在目标方法开始之前执行 @Before("execution(public int com.spring.aop.impl.ArithmeticCalculator.*(int, int))") public void beforeMethod(JoinPoint joinPoint) { String methodName = joinPoint.getSignature().getName(); List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("The method " + methodName + " begins with " + args);
##### 7、AspectJ 支持 5 种类型的通知注解:
@Before:前置通知,在方法执行之前执行
/** * 在 com.spring.aop.ArithmeticCalculator 接口的每一个实现类的每一个方法开始之前执行一段代码 * @param joinPoint */ @Before("execution(public int com.spring.aop.ArithmeticCalculator.*(..))") public void beforeMethod(JoinPoint joinPoint) { String methodName = joinPoint.getSignature().getName(); List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("The method " + methodName + " begins with " + args); }
@After:后置通知,在方法执行之后执行,即连接点返回结果或者抛出异常的时候,下面的后置通知记录了方法的终止。
/** * 在方法执行之后执行的代码,无论该方法是否出现异常 * @param joinPoint */ @After("execution(public int com.spring.aop.ArithmeticCalculator.*(..))") public void afterMethod(JoinPoint joinPoint) { String methodName = joinPoint.getSignature().getName(); System.out.println("The method " + methodName + " ends."); }
@AfterRunning:返回通知,在方法返回结果之后执行
/** * 在方法正常结束后执行的代码 * 返回通知是可以访问到方法的返回值的 */ @AfterReturning(value = "execution(public int com.spring.aop.ArithmeticCalculator.*(..))", returning = "result") public void afterReturning(JoinPoint joinPoint, Object result) { String methodName = joinPoint.getSignature().getName(); System.out.println("The method " + methodName + " ends with " + result); }
@AfterThrowing:异常通知,在方法抛出异常之后执行
/** * 在目标方法出现异常时会执行的代码 * 可以访问到异常对象;且可以指定在出现特定异常时再执行通知代码 * @param joinPoint * @param ex */ @AfterThrowing(value = "execution(public int com.spring.aop.ArithmeticCalculator.*(..))", throwing = "ex") public void afterThrowing(JoinPoint joinPoint, Exception ex) { String methodName = joinPoint.getSignature().getName(); System.out.println("The method " + methodName + " occurs exception: " + ex); }
@Around:环绕通知,围绕着方法执行
/** * 环绕通知需要携带 ProceedingJoinPoint 类型的参数 * 环绕通知类似于动态代理的全过程:ProceedingJoinPoint 这个类型的参数可以决定是否执行目标方法 * 且环绕通知必须有返回值,返回值即为目标方法的返回值 * @param pjd */ @Around("execution(public int com.spring.aop.ArithmeticCalculator.*(..))") public Object aroundMethod(ProceedingJoinPoint pjd) { Object result = null; String methodName = pjd.getSignature().getName(); //执行目标方法 try { //前置通知 System.out.println("The method " + methodName + " begins with " + Arrays.asList(pjd.getArgs())); result = pjd.proceed(); //返回通知 System.out.println("The method " + methodName + " ends with " + result); } catch (Throwable ex) { //异常通知 System.out.println("The method " + methodName + " occurs exception: " + ex); } //后置通知 System.out.println("The method " + methodName + " ends."); return result; }
8、指定切面的优先级
- 在同一个连接点上应用不止一个切面时,除非明确指定,否则它们的优先级是不确定的。
- 切面的优先级可以通过实现 Ordered 接口或者利用 @Order 注解指定。
- 实现 Ordered 接口,getOrder() 方法的返回值越小,优先级越高。
若使用 @Order 注解,序号出现在注解中。
/** * 可以使用 @Order 注解指定切面的优先级,值越小优先级越高 */ @Order(1) @Aspect @Component public class VlidationAspect { @Before("execution(public int com.spring.aop.ArithmeticCalculator.*(..))") public void validateArgs(JoinPoint joinPoint) { System.out.println("validate: " + Arrays.asList(joinPoint.getArgs())); } }
9、重用切点表达式
使用 @PointCut 来声明切入点表达式。
后面的其他通知直接使用方法名来引用当前的切入点表达式。
/** * 定义一个方法,用于声明切入点表达式。一般地,该方法不再需要舔入其他代码 */ @Pointcut("execution(public int com.spring.aop.ArithmeticCalculator.*(..))") public void declareJoinPointExpression() {}
@Before("declareJoinPointExpression()")
public void validateArgs(JoinPoint joinPoint) {
System.out.println("validate: " + Arrays.asList(joinPoint.getArgs()));
}
##### 10、基于配置文件的方法来配置 AOP
<?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: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/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 配置 bean -->
<bean id="arithmeticCalculator" class="com.spring.xml.ArithmeticCalculatorImpl"/>
<!-- 配置切面的 bean -->
<bean id="loggingAspect" class="com.spring.xml.LoggingAspect"/>
<bean id="vlidationAspect" class="com.spring.xml.VlidationAspect"/>
<!-- 配置 AOP -->
<aop:config>
<!-- 配置切点表达式 -->
<aop:pointcut id="pointcut" expression="execution(public int com.spring.xml.ArithmeticCalculator.*(..))"/>
<!-- 配置切面及通知 -->
<aop:aspect ref="loggingAspect" order="2">
<aop:before method="beforeMethod" pointcut-ref="pointcut"/>
<aop:after method="afterMethod" pointcut-ref="pointcut"/>
<aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="ex"/>
<aop:around method="aroundMethod" pointcut-ref="pointcut"/>
</aop:aspect>
<aop:aspect ref="vlidationAspect" order="1">
<aop:before method="validateArgs" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>