Spring/AOP
AOP(Aspect Orient Programming),也就是面向切面编程。面向对象编程(OOP)是从静态角度考虑程序结构,面向切面编程(AOP)是从动态角度考虑程序运行过程。
常常通过 AOP 来处理一些具有横切性质的系统性服务。
AOP 实际上是由目标类的代理类实现的。AOP 代理其实是由 AOP 框架动态生成的一个对象,该对象可作为目标对象使用。AOP 代理包含了目标对象的全部方法,但 AOP 代理中的方法与目标对象的方法存在差异,AOP 方法在特定切入点添加了增强处理,并回调了目标对象的方法。
Spring 中 AOP 代理由 Spring 的 IoC 容器负责生成、管理,其依赖关系也由 IoC 容器负责管理。因此,AOP 代理可以直接使用容器中的其他 Bean 实例作为目标,这种关系可由 IoC 容器的依赖注入提供。Spring 默认使用 Java 动态代理来创建 AOP 代理, 这样就可以为任何接口实例创建代理了。当需要代理的类不是代理接口的时候,Spring
自动会切换为使用 CGLIB 代理,也可强制使用 CGLIB。
代理对象的方法 = 增强处理 + 被代理对象的方法。
String/AOP的实现
AOP 编程
- 定义普通业务组件。
- 定义切入点,一个切入点可能横切多个业务组件。
- 定义增强处理,增强处理就是在 AOP 框架为普通业务组件织入的处理动作。
Spring 有如下两种选择来定义切入点和增强处理。
- 基于 Annotation 的“零配置”方式:使用@Aspect、@Pointcut等 Annotation 来标注切入点和增强处理。
- 基于 XML 配置文件的管理方式:使用 Spring 配置文件来定义切入点和增强点。
<?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">
<!-- 启动对@AspectJ注解的支持 -->
<aop:aspectj-autoproxy/>
<span style="font-family: Arial, Helvetica, sans-serif;"> <bean </span><span class="keyword" style="font-family: Arial, Helvetica, sans-serif;">class</span><span style="font-family: Arial, Helvetica, sans-serif;">=</span><span class="string" style="font-family: Arial, Helvetica, sans-serif;">"org.springframeword.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"</span><span style="font-family: Arial, Helvetica, sans-serif;"> /> </span>
</beans>
// 定义一个切面
@Aspect
public class BeforeAdviceTest {
// 匹配 com.wicresoft.app.service.impl 包下所有类的所有方法作为切入点
@Before("execution(* com.wicresoft.app.service.impl.*.*(..))")
public void authorith(){
System.out.println("模拟进行权限检查。");
}
}
使用@Before Annotation 时,指定切入点表达式。
(5)定义 AfterThrowing 增强处理(抛出异常)
After 增强处理与AfterReturning 增强处理有点相似,但也有区别:
- AfterReturning 增强处理处理只有在目标方法成功完成后才会被织入。
- After 增强处理不管目标方法如何结束(保存成功完成和遇到异常中止两种情况),它都会被织入。
public void aroundTest(ProceedingJointPoint jp) { //该处可以有多个参数,ProceedingJointPoint 是必须要有的参数
System.out.println("目标方法执行前...");
String ret= jp.proceed(new String[]{"被改变的参数"});//执行目标方法,通过传入参数来修改目标函数的参数,但是参数个数和类型必须和目标方法对于,否则报错
System.out.println("返回值:"+ret);
}
Spring 切入点定义包含两个部分:
- 一个切入点表达式。
- 一个包含名字和任意参数的方法签名。
// 使用@Pointcut Annotation 时指定切入点表达式
@pointcut("execution * transfer(..)")
// 使用一个返回值为void,方法体为空的方法来命名切入点
private void anyOldTransfer(){}
// 使用上面定义的切入点
@AfterReturning(pointcut="anyOldTransfer()", returning="reVal")
public void writeLog(String msg, Object reVal){
...
}
访问目标方法
定义增强处理方法时将第一个参数定义为 JoinPoint 类型,当该增强处理方法被调用时,该 JoinPoint 参数就代表了织入增强处理的连接点。
JoinPoint用法
Object[] getArgs(): 返回执行目标方法时的参数。
二、基于 XML 配置文件的管理方式
1.不配置切入点
<?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>
2.配置切入点
<?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>
<!-- 定义一个切入点,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>
<!-- 省略各个Bean 的配置 -->
<!-- ... -->
</beans>