AOP(Aspect Oriented Programming)
面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。
- 主要的功能是:日志记录、性能统计、安全控制、事务处理、异常处理等等
AOP实现方式
预编译
- AspectJ
运行期动态代理(JDK动态代理、CGLib动态代理)
- SpringAOP、JbossAOP
AOP的相关概念
- 切面(Aspect):一个关注点的模块化,这个关注点可能会横切多个对象
- 连接点(Joinpoint):程序执行过程中的某个特定的点
- 通知(Advice):在切面的某个特定的连接点上执行的动作
- 切入点(Pointcut):匹配连接点的断言,在AOP中通知和一个切入点表达式关联
- 引入(Introduction):在不修改代码的前提下,为类添加新的方法和属性
- 目标对象(Target Object):被一个或者多个切面所通知的对象
- AOP代理(AOP):AOP框架创建的对象,用来实现切面契约(aspect contract)(包括通知方法执行等功能)
- 织入(Weaving):把切面连接到其他的应用程序类型或者对象上,并创建一个被通知的对象,分为:编译时织入、类加载时织入、执行时织入
Advice的类型
- 前置通知(Before advice):在某连接点(join point)之前执行的通知,但不能阻止连接点前的执行(除非它抛出一个异常)
- 返回后通知(After returning advice):在某连接点(join point)正常完成后执行的通知
- 抛出异常后通知(After throwing advice):在方法抛出异常退出时执行的通知
- 后通知(After(finally) advice):当某连接点退出的时候执行的通知(不论是正常返回还是异常返回)
- 环绕通知(Around Advice):包围一个连接点(join point)的通知
Spring框架中AOP的用途
- 提供了声明式的企业服务,特别是EJB的替代服务的声明
- 允许用户定制自己的方面,以完成OOP与AOP的互补使用
Spring的AOP实现
- 纯Java实现,无需特殊的编译过程,不需要控制类加载器层次
- 目前只支持方法执行连接点(通知Spring Bean的方法执行)
- 不是为了提供最完整的AOP实现(尽管它非常强大);而是侧重于提供一种AOP实现和Spring IOC容器之间的整合,用于帮助解决企业应用中的常见问题
- Spring AOP不会与AspectJ竞争,从而提供综合全面的AOP解决方案
有接口和无接口的Spring AOP实现区别
- Spring AOP默认使用标准的JavaSE动态代理作为AOP代理,这使得任何接口(或者接口集)都可以被代理
- Spring AOP中也可以使用CGLIB代理(如果一个业务对象并没有实现一个接口)
基于Schema-based的AOP实现(基于配置的AOP实现)
Spring所有的切面和通知器都必须放在一个内(可以陪着包含多个元素),每一个可以包含pointcut、advisor和aspect元素(它们必须按照这个顺序进行声明)。风格的配置大量使用了Spring的自动代理机制。
- aspect
<bean id="moocAspect" class="com.imooc.aop.schema.advice.MoocAspect"></bean>
<bean id="aspectBiz" class="com.imooc.aop.schema.advice.biz.AspectBiz"></bean>
<aop:config>
<aop:aspect id="moocAspectAOP" ref="moocAspect">
</aop:aspect>
</aop:config>
- pointcut
- execution(public **(..)):切入点为执行所有public方法
- execution(* set *(..)):切入点为执行所有set开始的方法
- execution(* com.xyz.service.AccountService.*(..)):切入点为执行AccountService类中的所有方法时
- execution(* com.xyz.service..(..)):切入点为执行com.xyz.service包下的所有方法时
- execution(* com.xyz.service…(..)):切入点为执行com.xyz.service包及其子包下的所有方法时
- within(com.xyz.service.*):(only in Spring AOP)
- within(com.xyz.service..*):(only in Spring AOP):withiin用于匹配指定类型内的方法执行
- this(com.xyz.service.AccountService)(only in Spring AOP):this用于匹配当前AOP代理对象类型的执行方法
- target(com.xyz.service.AccountService)(only iin Spring AOP):target用于匹配当前目标对象类型的执行方法
- args(java.io.Serializable)(only in Spring AOP):args用于匹配当前执行的方法传入的参数为指定类型的执行方法
- @target(org.springframework.transaction.annotion.Transsactional)(only in Spring AOP)
- @within(org.springframework.transaction.annotion.Transsactional)(only in Spring AOP)
- @annotation(org.springframework.transaction.annotion.Transsactional)(only in Spring AOP)
- @args(com.xyz.security.Classified)(only in Spring AOP)
- bean(tradeService)(only in Spring AOP)
- bean(*Service)(only in Spring AOP)
<aop:config>
<aop:aspect id="moocAspectAOP" ref="moocAspect">
<aop:pointcut id="moocPiontcut"
expression="execution(* com.imooc.aop.schema.advice.biz.*Biz.*(..))" />
</aop:aspect>
</aop:config>
advice
- Before advice
<aop:config>
<aop:aspect id="moocAspectAOP" ref="moocAspect">
<aop:before pointcut="execution(* com.imooc.aop.schema.advice.biz.*Biz.*(..))" />
</aop:aspect>
</aop:config>
或者
<aop:config>
<aop:aspect id="moocAspectAOP" ref="moocAspect">
<aop:before method="before" pointcut-ref="moocPiontcut"/>
</aop:aspect>
</aop:config>
- After returning advice
<aop:config>
<aop:aspect id="moocAspectAOP" ref="moocAspect">
<aop:after-returning method="afterReturning" pointcut-ref="moocPiontcut"/>
</aop:aspect>
</aop:config>
或者
<aop:config>
<aop:aspect id="moocAspectAOP" ref="moocAspect">
<aop:after-returning
method="doAccessCheck"
pointcut-ref="dataAccessOperation"
returning="retVal"/>
</aop:aspect>
</aop:config>
- After throwing advice
<aop:config>
<aop:aspect id="moocAspectAOP" ref="moocAspect">
<aop:after-throwing method="afterThrowing" pointcut-ref="moocPiontcut"/>
</aop:aspect>
</aop:config>
使用throwing属性来指定可被传递的异常的参数名称
<aop:config>
<aop:aspect id="moocAspectAOP" ref="moocAspect">
<aop:after-throwing
method="doRecoveryActions"
pointcut-ref="dataAccessOperation"
throwing="dataAccessEx"/>
</aop:aspect>
</aop:config>
- After (finally) advice
<aop:config>
<aop:aspect id="moocAspectAOP" ref="moocAspect">
<aop:after method="after" pointcut-ref="moocPiontcut"/>
</aop:aspect>
</aop:config>
- around advice
-通知方法的第一个参数必须是ProceedingJoinPoing类型
<aop:config>
<aop:aspect id="moocAspectAOP" ref="moocAspect">
<aop:around method="around" pointcut-ref="moocPiontcut"/>
</aop:aspect>
</aop:config>
advice parameter
<aop:config>
<aop:aspect id="moocAspectAOP" ref="moocAspect">
<aop:around method="aroundInit" pointcut="execution(* com.imooc.aop.schema.advice.biz.AspectBiz.init(String, int)) and args(bizName, times)"/>
</aop:aspect>
</aop:config>
Introductions
- 简介允许一个切面声明一个实现指定接口的通知对象,并且提供了一个接口实现类来代表这些对象
- 由中的元素声明该元素用于声明所匹配的类型拥有一个新的parent(因此得名)
<aop:config>
<aop:aspect id="moocAspectAOP" ref="moocAspect">
<aop:declare-parents types-matching="com.imooc.aop.schema.advice.biz.*(+)"
implement-interface="com.imooc.aop.schema.advice.Fit"
default-impl="com.imooc.aop.schema.advice.FitImpl"/>
</aop:aspect>
</aop:config>
- Advisors
- advisor就像一个小的自包含的方面,只有一个advice
- 切面自身通过一个bean表示,并且必须实现某个advice接口,同时,advisor也可以很好的利用AspectJ切入点表达式
- Spring通过配置文件中元素支持advisor,实际使用中,大多数情况下它会和transactional advice配合使用
- 为了定义一个advisor的优先级以便让advice可以有序,可以使用order属性来定义advisor的顺序。
<context:component-scan base-package="com.imooc.aop.schema"></context:component-scan>
<aop:config>
<aop:aspect id="concurrentOperationRetry" ref="concurrentOperationExecutor">
<aop:pointcut id="idempotentOperation"
expression="execution(* com.imooc.aop.schema.advisors.service.*.*(..)) " />
<!-- expression="execution(* com.imooc.aop.schema.service.*.*(..)) and -->
<!-- @annotation(com.imooc.aop.schema.Idempotent)" /> -->
<aop:around pointcut-ref="idempotentOperation" method="doConcurrentOperation" />
</aop:aspect>
</aop:config>
<bean id="concurrentOperationExecutor" class="com.imooc.aop.schema.advisors.ConcurrentOperationExecutor">
<property name="maxRetries" value="3" />
<property name="order" value="100" />
</bean>