项目概况

第06篇:AOP面向切面编程

2026-01-299 min read后端技术
Spring Framework

公众号: 西魏陶渊明
CSDN: https://springlearn.blog.csdn.net

天下代码一大抄, 抄来抄去有提高, 看你会抄不会抄!

一、前言

面向切面编程 (AOP) 通过提供另一种思考程序结构的方式来补充面向对象编程 (OOP)。

OOP 中模块化的关键单元是类,而 AOP 中模块化的单元是切面。切面支持跨多种类型和对象的关注点(例如事务管理)的模块化。(这种关注点在 AOP 文献中通常被称为“横切”关注点。)

Spring 的关键组件之一是 AOP 框架。虽然 Spring IoC 容器不依赖 AOP(这意味着如果您不想使用 AOP,则无需使用 AOP),AOP 补充了 Spring IoC 以提供非常强大的中间件解决方案。

AOP 在 Spring Framework 中用于:

  1. 提供声明式事务管理。(TransactionalRepositoryProxyPostProcessor#postProcess)
  2. 让用户实现自定义切面,用 AOP 补充他们对 OOP 的使用。
    • 常用于接口日志打印
    • 或是基于接口的权限校验、数据预处理等操作

下面我们就学习下spring中aop的用法, 不用死记硬背, 了解收藏, 遇到会用即可。

二、AOP的概念

2.1 Aspect 切面

Aspect: 方面、切面,如果说oop是看整体,那么aop就是看一个面。

Spring 提供两种使用方法,1是基于注解方式, 2是基于配置方式。

2.1.1 注解方式

Spring会从被@Aspect修饰的类中读取切面信息,为符合条件的对象生成代理类。

java
1@Aspect 2public class NotVeryUsefulAspect {}

2.1.2 xml方式

xml
1<aop:config> 2 <aop:aspect id="myAspect" ref="aBean"> 3 ... 4 </aop:aspect> 5</aop:config> 6 7<bean id="aBean" class="..."> 8 ... 9</bean>

因为目前使用SpringBoot的较多,使用配置的方式的比较少,本文也只介绍使用注解方式。如果想了解xml配置方式,可以参考官方文档。

2.2 Join point 连接点

Join point(连接点):程序执行过程中的一个点,例如方法的执行或异常的处理。在 Spring AOP 中,一个连接点总是代表一个方法执行。

  • JoinPoint 基类方法,可以获取如下信息。
  • ProceedingJoinPoint 公开proceed(..)方法,以支持@AJ方面的around通知

我们可以通过连接点来获取当前执行的方法的信息,具体如下。

2.2.1 接口声明

java
1public interface JoinPoint { 2 // 当前执行的切面,或者说是拦截的方法 3 String toString(); 4 // 连接点的缩写字符串表示形式。 5 String toShortString(); 6 // 连接点的扩展字符串表示形式。 7 String toLongString(); 8 // 返回当前正在执行的对象。这将始终与@this切入点指示符匹配的对象相同。 9 // 除非您特别需要这种反射访问,否则您应该使用this切入点指示符来获取该对象, 10 // 以获得更好的静态类型和性能。 当当前没有可用的执行对象时返回null。 11 // 这包括在静态上下文中出现的所有连接点。 12 // 当前正在执行的对象(如果不可用则为空——例如静态上下文) 13 Object getThis(); 14 // 返回目标对象。这将始终与目标切入点指示符匹配的对象相同。 15 Object getTarget(); 16 // 这个连接点上的参数 17 Object[] getArgs(); 18 // 连接的执行方法签名,可以获取到执行的方法 19 Signature getSignature(); 20 // 源代码信息,比如获取执行的行,列,文件名。 21 SourceLocation getSourceLocation(); 22 // 当前的拦击的类型。 23 String getKind(); 24 // 封装此连接点的静态部分的对象。 25 StaticPart getStaticPart(); 26}

2.2.2 实践

java
1@Component 2public class AopDemoService { 3 4 public void say(String name) { 5 System.out.println("AopDemoService Say:" + name); 6 } 7} 8 9@Aspect 10@Component 11public class AspectManager { 12 13 @Before(value = "within(AopDemoService)") 14 public void beforeArgs(JoinPoint joinPoint) { 15 System.out.println("拦截器逻辑----------------------------"); 16 // execution(void learn.spring.aop.AopDemoService.say(String)) 17 System.out.println(joinPoint.toString()); 18 // execution(AopDemoService.say(..)) 19 System.out.println(joinPoint.toShortString()); 20 // execution(public void learn.spring.aop.AopDemoService.say(java.lang.String)) 21 System.out.println(joinPoint.toLongString()); 22 // method-execution 23 System.out.println(joinPoint.getKind()); 24 // [Jay] 25 System.out.println(Arrays.toString(joinPoint.getArgs())); 26 // learn.spring.aop.AopDemoService@73c9e8e8 27 System.out.println(joinPoint.getTarget()); 28 // learn.spring.aop.AopDemoService@73c9e8e8 29 System.out.println(joinPoint.getThis()); 30 // void learn.spring.aop.AopDemoService.say(String) 31 System.out.println(joinPoint.getSignature()); 32 // execution(void learn.spring.aop.AopDemoService.say(String)) 33 System.out.println(joinPoint.getStaticPart()); 34 // org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint$SourceLocationImpl@582a764a 35 System.out.println(joinPoint.getSourceLocation()); 36 System.out.println("拦截器逻辑----------------------------"); 37 } 38 39} 40 41

2.3 Advice 通知

通过使用 Advice通知,我们可以在方法执行的各个阶段植入我们的自定义代码。具体有哪些通知呢?可以参考下文。

注解说明
@Before前置通知, 在方法执行之前执行
@After后置通知, 在方法执行之后执行
@AfterRunning返回通知 在方法返回结果之后执行
@AfterThrowing异常通知在方法抛出异常之后
@Around环绕通知, 围绕着方法执行

2.3.1 Before Advice

通过前置请求,我们可以获取请求参数和请求方法等信息。 在连接点之前运行但不能阻止执行流继续到连接点的通知(除非它抛出异常)。

java
1@Aspect 2@Component 3public class AspectManager { 4 @Before(value = "within(AopDemoService)") 5 public void beforeArgs(JoinPoint joinPoint) { 6 System.out.println("拦截器逻辑----------------------------"); 7 System.out.println(joinPoint.toString()); 8 System.out.println(joinPoint.toShortString()); 9 System.out.println(joinPoint.toLongString()); 10 System.out.println(joinPoint.getKind()); 11 System.out.println(Arrays.toString(joinPoint.getArgs())); 12 System.out.println(joinPoint.getTarget()); 13 System.out.println(joinPoint.getThis()); 14 System.out.println(joinPoint.getSignature()); 15 System.out.println(joinPoint.getStaticPart()); 16 System.out.println(joinPoint.getSourceLocation()); 17 System.out.println("拦截器逻辑----------------------------"); 18 } 19}

2.3.2 After Advice

在连接点正常完成后运行的通知(例如,如果方法返回而没有引发异常)。【注意如果方法异常,则不执行】 @After注释声明的方法,会在连接方法执行退出时(finally)执行。所以必须准备好处理正常和异常返回条件。它通常用于释放资源和类似的目的。下面的例子展示了如何使用after finally advice:

java
1import org.aspectj.lang.annotation.Aspect; 2import org.aspectj.lang.annotation.After; 3 4@Aspect 5public class AfterFinallyExample { 6 7 @After("com.xyz.myapp.CommonPointcuts.dataAccessOperation()") 8 public void doReleaseLock() { 9 // ... 10 } 11}

2.3.3 AfterRunning Advice

无论连接点以何种方式退出(正常或异常返回),都将运行建议。

有时,您需要在通知体中访问返回的实际值。你可以使用@ afterreturns的形式绑定返回值来获得访问权限,如下例所示:

返回属性中使用的名称必须与advice方法中的参数名称对应。当方法执行返回时,返回值作为对应的参数值传递给advice方法。返回子句还将匹配限制为仅那些返回指定类型值的方法执行(在本例中为Object,它匹配任何返回值)。

请注意,在返回advice之后使用时,不可能返回一个完全不同的引用。

java
1@Aspect 2@Component 3public class AopArgsMatchProcessor { 4 5 @AfterReturning(value = "within(Person) && args(name,age)", returning = "retValue") 6 public void beforeArgs(Integer age, String name, String retValue) { 7 System.out.println("拦截器逻辑----------------------------"); 8 System.out.println("入参name:" + name); 9 System.out.println("入参age:" + age); 10 System.out.println("返回值:" + value); 11 System.out.println("拦截器逻辑----------------------------"); 12 } 13}

2.3.4 AfterThrowing Advice

如果方法因抛出异常而退出,则运行。

当匹配的方法执行通过抛出异常退出时,将运行通知。你可以使用@ afterthrows注释来声明它,如下面的例子所示:

通常,您希望仅在抛出给定类型的异常时才运行通知,而且通常还需要在通知主体中访问抛出的异常。您可以使用throw属性来限制匹配(如果需要,则使用Throwable作为异常类型),并将抛出的异常绑定到通知参数。下面的例子展示了如何做到这一点:

java
1import org.aspectj.lang.annotation.Aspect; 2import org.aspectj.lang.annotation.AfterThrowing; 3 4@Aspect 5public class AfterThrowingExample { 6 7 @AfterThrowing( 8 pointcut="com.xyz.myapp.CommonPointcuts.dataAccessOperation()", 9 throwing="ex") 10 public void doRecoveryActions(DataAccessException ex) { 11 // ... 12 } 13}

2.3.5 Around Advice

Around Advice: 环绕通知

Around通知在匹配方法的执行过程中“绕”运行。它有机会在方法运行之前和之后都做一些工作,并决定什么时候、如何,甚至是否真的要运行方法。如果您需要以线程安全的方式在方法执行前后共享状态(例如,启动和停止计时器),则经常使用Around通知。

环绕通知可以在方法调用之前和之后执行自定义行为。它还负责选择是继续到连接点还是通过返回自己的返回值或抛出异常来缩短建议的方法执行。

java
1 @Around("controllerLog()&& @annotation(logAnnotation)") 2 public Object doAround(ProceedingJoinPoint joinPoint, ApiOperation logAnnotation) { 3 try { 4 Long startTime = this.doBefore(joinPoint, logAnnotation); 5 Object result = joinPoint.proceed(); 6 this.doAfterReturning(result, logAnnotation, startTime); 7 return result; 8 } catch (Throwable var5) { 9 return this.doAfterThrowingAdvice(joinPoint, logAnnotation, var5); 10 } 11 }

2.4 Pointcut 切面表达式

切面,Pointcut 配合 Advice 才有用, Advice只有在遇到Pointcut指定的类或者方法上才会执行。spring中Pointcut提供了灵活的匹配表达式。我们日常中最常用的就是 execution ,但是实际上匹配表达式还包含了很多的注解。如下图表格。这些你都见过吗?

注解说明
within拦截指定类及指定包下所有的类
@within拦截被指定注解修饰的类
this拦截指定的类型
bean执行的bean中,支持模糊匹配 *Service
args拦截指定参数类型的方法,同时可以传递参数
@annotation拦截带指定注解的方法
@args拦截方法入参被中@args指定的注解(入参只能有一个)
@target拦截方法被指定注解修饰的方法
execution表达式详情见下文

这里先做简单的介绍,因为内容较多,更多的实践见下文,三,切面表达式最佳实践。

2.4.1 注解匹配

凡是带@符号的,都是用来匹配注解的,如下,这些注解,可以用来匹配注解修饰的: 类,方法,参数.

  • @within,拦截被指定注解,修饰的类

如下 AopDemoService@CustomerAnnotation 修饰,所以就可以使用 @within 匹配到。

java
1@Component 2@CustomerAnnotation 3public class AopDemoService { 4 public void say(String name) { 5 System.out.println("AopDemoService Say:" + name); 6 } 7 public void sayAge(Integer age) { 8 System.out.println("AopDemoSerCue sayAge:" + age); 9 } 10 public void sayPerson(Person person) { 11 System.out.println("AopDemoSerCue Person:" + person); 12 } 13} 14@Aspect 15@Component 16public class AspectConfig { 17 @After(value = "@within(CustomerAnnotation)") 18 public void matchAnno() { 19 System.out.println("CustomerAnnotation @within 修饰类"); 20 } 21} 22
  • @target,跟@within是一样的,功能。不同的是,@within会把被注解标记的目标对象及其子类的方法都进行拦截,而@target只会拦截目标对象。
  • @annotation,拦截被指定注解,修饰的方法
java
1@Aspect 2@Component 3public class AspectConfig { 4 @After(value = "within(AopDemoService) && @annotation(CustomerAnnotation)") 5 public void matchAnnoAn() { 6 System.out.println("CustomerAnnotation @target 任意连接点"); 7 } 8} 9@Component 10public class AopDemoService { 11 @CustomerAnnotation 12 public void say(String name) { 13 System.out.println("AopDemoService Say:" + name); 14 } 15}
  • @args,拦截方法入参被中@args指定的注解

如下 Person 被 @CustomerAnnotation 修饰,故可以使用 @args 匹配到

java
1@CustomerAnnotation 2public class Person { 3} 4@Component 5public class AopDemoService { 6 public void sayPerson(Person person) { 7 System.out.println("AopDemoSerCue Person:" + person); 8 } 9} 10@Aspect 11@Component 12public class AspectConfig { 13 @Before(value = "this(AopDemoService) && @args(customerAnnotation)") 14 public void matchAnno1(CustomerAnnotation customerAnnotation) { 15 System.out.println("CustomerAnnotation @args 修饰方法参数"); 16 } 17}

2.4.2 参数传递

使用 args 可以将参数进行传递。如下。

java
1@Component 2public class AopDemoService { 3 public void sayPerson(Person person, Integer count) { 4 System.out.println("AopDemoSerCue " + count + "Person:" + person); 5 } 6} 7@Aspect 8@Component 9public class AspectConfig { 10 // args中是变量名,会自动从当前方法的入参中找到这个变量名对应的实际类型。 11 @After(value = "within(AopDemoService) && args(person,age,..)") 12 public void args(Person person, Integer age) { 13 System.out.println("指定匹配参数"); 14 } 15}

2.5 Target object 目标、代理类

一个或多个切面建议的对象。也称为“目标对象”。由于 Spring AOP 是使用运行时代理实现的,因此该对象始终是代理对象。

java
1public abstract class AbstractAdvisingBeanPostProcessor extends ProxyProcessorSupport implements BeanPostProcessor { 2 3 @Override 4 public Object postProcessAfterInitialization(Object bean, String beanName) { 5 ... 6 if (isEligible(bean, beanName)) { 7 ProxyFactory proxyFactory = prepareProxyFactory(bean, beanName); 8 if (!proxyFactory.isProxyTargetClass()) { 9 evaluateProxyInterfaces(bean.getClass(), proxyFactory); 10 } 11 proxyFactory.addAdvisor(this.advisor); 12 customizeProxyFactory(proxyFactory); 13 return proxyFactory.getProxy(getProxyClassLoader()); 14 } 15 16 // No proxy needed. 17 return bean; 18 } 19}

2.6 AOP proxy 代理工具

由 AOP 框架创建的对象。在 Spring Framework 中,AOP 代理是 JDK 动态代理或 CGLIB 代理。

我们可以使用下面这段代码, 来生成代理类, 而具体是用jdk还是cglib这些就交给底层自己去选择。更多的实践, 看下文。

java
1 @Test 2 public void testProxy() throws Exception { 3 AopTest aopTest = new AopTest(); 4 ProxyFactory proxyFactory = new ProxyFactory(aopTest); 5 6 proxyFactory.addAdvisor(defaultPointcutAdvisor); 7 proxyFactory.setExposeProxy(true); 8 AopTest proxy = (AopTest) proxyFactory.getProxy(); 9 proxy.test(); 10 }

以上是spring aop的主要

三、切面表达式最佳实践

以下代码,全部已上传仓库: https://github.com/lxchinesszz/spring-learning

3.1 within

3.1.1 API说明

  1. 精确匹配类名
  2. 模糊匹配包中所有的类
  3. 模糊匹配包中所有的带Impl后缀的

3.1.2 目录

text
1└── WithinMatchProcessor 2 ├── AopWithinMatchProcessor.java 3 ├── CokeImpl.java 4 ├── Water.java 5 └── readme.md

3.1.3 拦截代码

java
1@Aspect 2@Component 3public class AopWithinMatchProcessor { 4 5 /** 6 * 精确匹配类名 7 */ 8 @Pointcut("within(spring.learning.aop.WithinMatchProcessor.Water)") 9 private void matchClassName() { 10 } 11 12 /** 13 * 模糊匹配包中所有的类 14 */ 15 @Pointcut("within(spring.learning.aop.WithinMatchProcessor.*)") 16 private void matchAllClassFromPackage() { 17 } 18 19 /** 20 * 模糊匹配包中所有的带Impl后缀的 21 */ 22 @Pointcut("within(spring.learning.aop.WithinMatchProcessor.*Impl)") 23 private void matchClassFromPackage() { 24 } 25 26 27 @Before("matchClassName()") 28 public void beforeMatchClassName() { 29 System.out.println("--------精确匹配类名-------"); 30 } 31 32 @Before("matchAllClassFromPackage()") 33 public void beforeMatchAllClassFormPackage() { 34 System.out.println("--------模糊匹配包中所有的类-------"); 35 } 36 37 @Before("matchClassFromPackage()") 38 public void beforeMatchClassFromPackage() { 39 System.out.println("--------模糊匹配包中所有的带Impl后缀的-------"); 40 } 41 42 43} 44

3.2 @within

3.2.1 API说明

拦截被指定注解标注的类

3.2.2 目录

text
1├── AnnotationWithinMatchProcessor 2│ ├── AopAnnotationWithinMatchProcessor.java 3│ ├── Log.java 4│ ├── Sprite.java 5│ └── readme.md 6

3.2.3 拦截代码

java
1@Log(tag = "SpriteLog") 2@Component 3public class Sprite { 4 5 public void drink() { 6 System.out.println("空参数"); 7 } 8 9 public void drink(Integer age) { 10 System.out.println("age"); 11 } 12 13 14 public String name() { 15 return "Sprite.name"; 16 } 17 18 public void toCalculate() throws Exception { 19 System.out.println(0 / 0); 20 } 21} 22 23@Aspect 24@Component 25public class AopAnnotationWithinMatchProcessor { 26 27 28 /** 29 * 注意可以将注解,放到参数中,此时@within()会将参数入参名去找到注解的类型 30 * 凡是被Log标记的类,都会被拦截 31 * 32 * @param spriteLog 注解 33 */ 34 @Before("@within(spriteLog)") 35 public void beforeAnnotationMatch(Log spriteLog) { 36 System.out.println("--------拦截被Log修饰类的所有方法" + spriteLog.tag() + "-------"); 37 } 38 39 40 /** 41 * 返回值 42 * 43 * @param value 返回值 44 * @param spriteLog 注解 45 */ 46 @AfterReturning(value = "@within(spriteLog)", returning = "value") 47 public void afterReturningAnnotationMatch(String value, Log spriteLog) { 48 System.out.println("afterReturningAnnotationMatch返回值:" + value + ",注解:" + spriteLog); 49 } 50 51 /** 52 * 拦截异常 53 * 54 * @param e 异常 55 * @param spriteLog 拦截日志 56 */ 57 @AfterThrowing(value = "@within(spriteLog)", throwing = "e") 58 public void AfterThrowingAnnotationMatch(Exception e, Log spriteLog) { 59 System.out.println(e.getMessage()); 60 } 61 62}

3.3 this

3.3.1 API说明

拦截指定的类

3.3.2 目录

text
1├── ThisMatchProcessor 2│ ├── AopThisMatchProcessor.java 3│ ├── ThisPerson.java 4│ └── readme.md 5

3.3.3 拦截代码

java
1@Aspect 2@Component 3public class AopThisMatchProcessor { 4 5 @Before(value = "this(ThisPerson)") 6 public void thisMatch() { 7 System.out.println("--------------ThisPerson------------"); 8 } 9} 10

3.4 args

3.4.1 API说明

java
1@Component 2public class Person { 3 4 public String info(String name) { 5 return "姓名:" + name; 6 } 7 8 public String info(String name, Integer age) { 9 return "姓名:" + name + ",年龄:" + age; 10 } 11}

Person类中有两个info方法,但是入参不一样,假如要拦截指定入参的方法时候,就可以使用args

3.4.2 目录

text
1├── ArgsMatchProcessor 2│ ├── AopArgsMatchProcessor.java 3│ ├── Person.java 4│ └── readme.md

3.4.3 拦截代码

可以看到args 和 within可以通过&&来进行,联合匹配。另外可以通过returning方法指定方法的返回值。但是注意,类型要和要拦截的方法的返回类型匹配。否则会报错。

java
1@Aspect 2@Component 3public class AopArgsMatchProcessor { 4 5 @AfterReturning(value = "within(Person) && args(name,age)", returning = "value") 6 public void beforeArgs(Integer age, String name, String value) { 7 System.out.println("拦截器逻辑----------------------------"); 8 System.out.println("入参name:" + name); 9 System.out.println("入参age:" + age); 10 System.out.println("返回值:" + value); 11 System.out.println("拦截器逻辑----------------------------"); 12 } 13}

3.5 @annotation

3.5.1 API说明

拦截被指定注解标记的方法。

3.5.2 目录

text
1├── AnnotationMethodMatchProcessor 2│ ├── AopAnnotationMethodMatchProcessor.java 3│ ├── LogMethod.java 4│ └── Main.java 5

3.5.3 代码

java
1@Aspect 2@Component 3public class AopAnnotationMethodMatchProcessor { 4 5 6 @Before(value = "@annotation(logMethod) && args(args)") 7 public void annotationMethodMatch(LogMethod logMethod, String args) { 8 System.out.println("注解方法匹配"); 9 } 10}

3.6 @args

3.6.1 API说明

拦截方法中入参被@args指定注解的方法。

3.6.2 目录

text
1├── AnnotationArgsMatchProcessor 2│ ├── AopAnnotationArgsMatchProcessor.java 3│ ├── Apple.java 4│ ├── Fruit.java 5│ ├── Orange.java 6│ └── Teacher.java

3.6.3 代码

注意当出现以下异常说明aop声明的拦截范围太广泛了,导致了一些不能拦截的类被拦截从而报错了,此时只用缩小拦截的范围即可

text
1 Cannot subclass final class org.springframework.boot.autoconfigure.AutoConfigurationPackages$BasePackages

缩小拦截范围如下使用this拦截指定类型

java
1@Aspect 2@Component 3public class AopAnnotationArgsMatchProcessor { 4 5 @Before(value = "@args(fruit) && this(Teacher)") 6 public void annotationMethodMatch(Fruit fruit) { 7 System.out.println("拦截被Fruit+tag:"+fruit.tag()); 8 } 9} 10

3.7 execution

3.7.1 API说明

execution()是最常用的切点函数,其语法如下所示:

execution(<修饰符模式>? <返回类型模式> <方法名模式>(<参数模式>) <异常模式>?) 除了返回类型模式、方法名模式和参数模式外,其它项都是可选的

表达式说明
execution(public * *(..))匹配所有目标类的public方法
execution(* *Test(..))匹配目标类所有以To为后缀的方法
execution(spring.learning.Water.(..))匹配Water接口所有方法
execution(spring.learning.Water+.(..))匹配Water接口以及实现类中所有方法(包括Water接口中没有的方法)
execution(* spring.learning.*(..))匹配spring.learning包下所有的类所有方法
execution(* spring.learning..*(..))匹配spring.learning包及其子孙包下所有的类所有方法
execution(* spring..*.Dao.find(..))匹配包名前缀为spring的任何包下类名后缀为Dao的方法,方法名必须以find为前缀
execution(* info(String,Integer))匹配info方法中,第一个参数是String,第二个Integer的方法
execution(* info(String,*)))匹配info方法中,第一个参数是String,第二个任意类型
execution(* info(String,..)))匹配info方法中,第一个参数是String,后面任意参数
execution(* info(Object+)))匹配info方法中,方法拥有一个入参,且入参是Object类型或该类的子类

四、编程式AOP

spring的aop功能如此强大, 难道只能用来写业务吗? 相信如果你要用aop这么灵活的东西来写业务代码,一定会被喷的。 因为太灵活了, 如果你不说,可能没人会发现,哎这块代码竟然有一个植入了切面逻辑。

所以我们学习这么多的, 终极目的是做中间件的开发, 这么强大的功能, 不好好利用,那不是亏了吗。就比如说aop, 这不就是java代理的增强吗? 要你自己做代理,你还要兼容jdk和cglib的场景,我们直接用spring的aop模块它不香吗?

所以下面我们学习如果编程是使用spring的aop能力。

java
1ProxyFactory factory = new ProxyFactory(myBusinessInterfaceImpl); 2factory.addAdvice(myMethodInterceptor); 3factory.addAdvisor(myAdvisor); 4MyBusinessInterface tb = (MyBusinessInterface) factory.getProxy();

4.1 Advice 通知

这种方法就跟代理一样,advice就好比jdk代理中的 InvocationHandler,是否需要执行,要看拦截器中如何处理。

  • ProxyFactory proxyFactory = new ProxyFactory(aopTest)
  • proxyFactory.addAdvice()

注意这种方法获取的代理类是针对所有的代理对象的方法。如果你想使用匹配的方式去精确的制定要连接那个方法,就不要这样使用。你要用 Advisor

java
1 @Test 2 public void test1() throws Exception { 3 AopTest aopTest = new AopTest(); 4 // 原始对象hashCode:124407148 5 System.out.println("原始对象hashCode:" + aopTest.hashCode()); 6 ProxyFactory proxyFactory = new ProxyFactory(aopTest); 7 proxyFactory.addAdvice(new MethodBeforeAdvice() { 8 @Override 9 public void before(Method method, Object[] args, Object target) throws Throwable { 10 System.out.println(method.getName() + "开始执行前"); 11 } 12 }); 13 proxyFactory.addAdvice(new MethodInterceptor() { 14 @Override 15 public Object invoke(MethodInvocation invocation) throws Throwable { 16 System.out.println(invocation.getMethod().getName() + "真正开始执行"); 17 return invocation.proceed(); 18 } 19 }); 20 AopTest proxy = (AopTest) proxyFactory.getProxy(); 21 // 代理对象hashCode:-487413954 22 System.out.println("代理对象hashCode:" + proxy.hashCode()); 23 TargetSource targetSource = proxyFactory.getTargetSource(); 24 // 获取原始对象hashCode:124407148 25 System.out.println("获取原始对象hashCode:" + targetSource.getTarget().hashCode()); 26 // test开始执行前 27 // test真正开始执行 28 // test 29 proxy.test(); 30 }

4.2 Advisor

Advisor 是支持切面匹配的, 通过设置切入点,来生成代理类。我们只用向 DefaultPointcutAdvisor 设置你的切面匹配器和 Advice 就行。如下。

java
1DefaultPointcutAdvisor defaultPointcutAdvisor = new DefaultPointcutAdvisor(); 2defaultPointcutAdvisor.setPointcut(new NameMatchMethodPointcut()); 3defaultPointcutAdvisor.setAdvice(...);

如果我们想自定义切面,那么只用实现这个切面的方法

java
1public interface Pointcut { 2 3 /** 4 * Return the ClassFilter for this pointcut. 5 * @return the ClassFilter (never {@code null}) 6 */ 7 ClassFilter getClassFilter(); 8 9 /** 10 * Return the MethodMatcher for this pointcut. 11 * @return the MethodMatcher (never {@code null}) 12 */ 13 MethodMatcher getMethodMatcher(); 14}

当然spring中给我们提供了很多可用的工具,但是为了加深大家的体感,这里我们就自定一个切面处理类吧。如下实例。

java
1 DefaultPointcutAdvisor defaultPointcutAdvisor = new DefaultPointcutAdvisor(); 2 defaultPointcutAdvisor.setPointcut(new Pointcut() { 3 @Override 4 public ClassFilter getClassFilter() { 5 // 类都匹配 6 return ClassFilter.TRUE; 7 } 8 9 @Override 10 public MethodMatcher getMethodMatcher() { 11 return new MethodMatcher() { 12 @Override 13 public boolean matches(Method method, Class<?> targetClass) { 14 return method.getName().equals("test007"); 15 } 16 17 @Override 18 public boolean isRuntime() { 19 return false; 20 } 21 22 @Override 23 public boolean matches(Method method, Class<?> targetClass, Object... args) { 24 return method.getName().equals("test007"); 25 } 26 }; 27 } 28 }); 29 defaultPointcutAdvisor.setAdvice(new MethodInterceptor() { 30 @Override 31 public Object invoke(MethodInvocation invocation) throws Throwable { 32 System.out.println(invocation.getMethod().getName() + "真正开始执行"); 33 return invocation.proceed(); 34 } 35 });

如果你绝的麻烦你也可以直接使用spring提供的切面工具。比如我们可以使用方法匹配工具。

java
1 DefaultPointcutAdvisor defaultPointcutAdvisor = new DefaultPointcutAdvisor(); 2 NameMatchMethodPointcut nameMatchMethodPointcut = new NameMatchMethodPointcut(); 3 nameMatchMethodPointcut.setMappedName("test007"); 4 nameMatchMethodPointcut.setClassFilter(ClassFilter.TRUE); 5 defaultPointcutAdvisor.setPointcut(nameMatchMethodPointcut);

然后执行下面代码获取代理类对象。

java
1 @Test 2 public void test2() throws Exception { 3 AopTest aopTest = new AopTest(); 4 ProxyFactory proxyFactory = new ProxyFactory(aopTest); 5 6 proxyFactory.addAdvisor(defaultPointcutAdvisor); 7 proxyFactory.setExposeProxy(true); 8 AopTest proxy = (AopTest) proxyFactory.getProxy(); 9 proxy.test(); 10 // 只拦截test007 11 proxy.test007(); 12 }

像这样的工具还有非常的多,毕竟Spring中切面非常的灵活,所以工具就非常的多。这个没比较都一一都掌握,了解就行。方便以后我们自己造轮子使用。但是如果你想了解的话,那就敬请期待下一课堂吧。

最后,都看到这里了,最后如果这篇文章,对你有所帮助,请点个关注,交个朋友。