大纲
什么是AOP?
基本概念
Spring中的AOP
Schema—based AOP基于配置、文件
SpringAOP API
AspectJ
面向切面编程Aspect Oriented Programming,通过预编译和运行期动态代理实现程序功能的统一维护的一种技术
主要功能:日志记录,性能统计,安全控制,事务处理,异常处理
实现方式:预编译(如AspectJ)
运行期动态代理(JDK动态代理(有接口)、CGLib动态代理(无接口)如:SpringAOP、JbossAOP)
提供了声明式的企业服务,特别是EJB的代替服务的使用;允许用户定制自己的方面,完成OOP与AOP的互补使用
不提供最完整的AOP实现,侧重提供AOP和Spring IoC容器之间的整合,用于帮助企业应用中的常见问题
基本概念
切面(Aspect):the modularization of a concern thatcuts across multiple classes
连接点(Join Point):a point during the execution ofa program
通知(Advice):action taken by a aspect at aparticular join point
切入点(Pointcut):a predicate that matches joinpoint
引入(Introduction):declaring additional methodsor fields on behalf of a type
目标对象(Target Objection):object being advised byone or more aspects
AOP代理(AOP proxy):an Object created by AOP framework in order to implement the aspectcontracts
织入(Weaving):linking aspect with otherapplication types or objects to create an advised object
Advice:
Beforeadvice: execution before a join point, cannot break(unless throws an exception)
Afterreturning advice: after join point completes normally
Afterthrowing advice: if method exits by throwing an exception
After(finally) advice:
Aroundadvice:
配置:
spring-aop-schema-advice.xml
<aop:config>
<aop:aspect id=”” ref=””>
<aop:pointcut id=””expression=””/>
<aop:before method=””point-ref=””>
<aop:after-returningmethod=”” point-ref=””>
<aop:after-throwingmethod=”” point-ref=””>
<aop:after method=””point-ref=””>
<aop:around method=””point-ref=””>
</aop:aspect>
</aop:config>
说明:
ref:跟配置bean的id
expression: pointcut有很多规则,用的时候再看
method:aop:aspect中的方法
point-ref:aop:pointcut的id
around的参数必须是ProceedingJoinPoint