一、AOP概念:
AOP(Aspect Orient Programming),也就是面向切面编程。
二、AOP术语:
通知(Advice):通知定义了切面是什么以及何时使用。除了描述切面要完成的工作,通知还解决了何时执行这个工作的问题。它应该在某个方法被调用之前?之后?之前和之后?或是在方法抛出异常时。
连接点(Joinpoint):连接点是在程序执行过程中能够插入切面的一个点。这个点可以是方法被调用时,异常被抛出时、甚至字段被编辑时。切面代码可以通过这些点插入到程序的一般流程中,从而添加新的行为。
切入点(Pointcut):如果通知定义了切面的“什么”和“何时”,那么切入点就定义了“何地”。切入点的定义匹配通知要织入一个或多个连接点。我们通常使用明确的类和方法名称,或利用正则表达式定义匹配的类和方法名称模板来指定这些切入点。
切面(Aspect):切面是切入点和通知的结合。通知和切入点共同定义了关于切面的全部内容——它的功能、在何时和何地完成其功能。
引入(Introduction):“引入”允许我们向现有的类添加新的方法或属性。
目标(target):被通知的对象。
代理(proxy):向目标对象应用通知之后被创建的对象。
织入(Weaving):织入是把切面应用到目标对象来创建新的代理对象的过程。
三、AOP原理:
如图,AOP 实际上是由目标类的代理类实现的。AOP 代理其实是由 AOP 框架动态生成的一个对象,该对象可作为目标对象使用。AOP 代理包含了目标对象的全部方法,但 AOP 代理中的方法与目标对象的方法存在差异,AOP 方法在特定切入点添加了增强处理,并回调了目标对象的方法。
四、Spring对AOP的支持:
五、Spring中AOP的实现
Spring 有如下两种选择来定义切入点和增强处理。
基于 Annotation 的“零配置”方式:使用@Aspect、@Pointcut等 Annotation 来标注切入点和增强处理。
基于 XML 配置文件的管理方式:使用 Spring 配置文件来定义切入点和增强点。
Spring的AOP里有5个类型的通知,用于指定相对于连接点何时运行通知。
Before(前),After-returning(返回后),After-throwing(抛出后),Aroud(周围),Introduction(引入)
①基于Annotation的“零配置”方式。
首先启用 Spring 对 @AspectJ 切面配置的支持。
<aop:aspectj-autoproxy/>
public class Audience {
public Audience() {}
@Pointcut("execution(* *.perform(..))")
public void performance() {}
@Before("performance()")
public void takeSeats(){
System.out.println("The audience is taking their seats.");
}
@Before("performance()")
public void turnOffCellPhones(){
System.out.println("The audience is turning off their cellphones.");
}
@AfterReturning("performance()")
public void applaud() {
System.out.println("CLAP CLAP CLAP CLAP CLAP CLAP");
}
@AfterThrowing("performance()")
public void demandRefund() {
System.out.println("Boo! We want our money back")
}
}
public void watchPerformance(ProceedingJoinPoint joinpoint){
System.out.println("The audience is taking their seats.");
System.out.println("The audience is turning off their cellphones.");
try{
joinpoint.proceed();
}catch(PerformanceException throwable){
System.out.println("CLAP CLAP CLAP CLAP CLAP CLAP");
}
}
切入点表达式”execution“格式如下:
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)
modifiers-pattern:方法的操作权限
ret-type-pattern:返回值
declaring-type-pattern:方法所在的包
name-pattern:方法名
parm-pattern:参数名
throws-pattern:异常
其中,除ret-type-pattern和name-pattern之外,其他都是可选的。上例中,execution(* com.spring.service.*.*(..))表示com.spring.service包下,返回值为任意类型;方法名任意;参数不作限制的所有方法。
②基于 XML 配置文件的管理方式
<?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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/beans/spring-aop-3.0.xsd">
<aop:config>
<!-- 将 fourAdviceBean 转换成切面 Bean, 切面 Bean 的新名称为:fourAdviceAspect,指定该切面的优先级为2 -->
<aop:aspect id="fourAdviceAspect" ref="fourAdviceBean" order="2">
<!-- 定义个After增强处理,直接指定切入点表达式,以切面 Bean 中的 Release() 方法作为增强处理方法 -->
<aop:after pointcut="execution(* com.wicresoft.app.service.impl.*.*(..))" method="release" />
<!-- 定义个Before增强处理,直接指定切入点表达式,以切面 Bean 中的 authority() 方法作为增强处理方法 -->
<aop:before pointcut="execution(* com.wicresoft.app.service.impl.*.*(..))" method="authority" />
<!-- 定义个AfterReturning增强处理,直接指定切入点表达式,以切面 Bean 中的 log() 方法作为增强处理方法 -->
<aop:after-returning pointcut="execution(* com.wicresoft.app.service.impl.*.*(..))" method="log" />
<!-- 定义个Around增强处理,直接指定切入点表达式,以切面 Bean 中的 processTx() 方法作为增强处理方法 -->
<aop:around pointcut="execution(* com.wicresoft.app.service.impl.*.*(..))" method="processTx" />
</aop:aspect>
</aop:config>
<!-- 省略各个Bean 的配置 -->
<!-- ... -->
</beans>
配置切入点
<aop:config>
<!-- 定义一个切入点,myPointcut,直接知道它对应的切入点表达式 -->
<aop:pointcut id="myPointcut" expression="execution(* com.wicresoft.app.service.impl.*.*(..))" method="release" />
<aop:aspect id="afterThrowingAdviceAspect" ref="afterThrowingAdviceBean" order="1">
<!-- 使用上面定于切入点定义增强处理 -->
<!-- 定义一个AfterThrowing 增强处理,指定切入点以切面 Bean 中的 doRecovertyActions() 方法作为增强处理方法 -->
<aop:after-throwing pointcut-ref="myPointcut" method="doRecovertyActions" throwing="ex" />
</aop:aspect>
</aop:config>