Spring Framework源码(五):Spring AOP简介
来源:互联网 发布:金十数据利空原油 编辑:程序博客网 时间:2024/05/09 22:03
Spring 中 AOP 代理由 Spring 的 IoC 容器负责生成、管理,其依赖关系也由 IoC 容器负责管理。因此,AOP 代理可以直接使用容器中的其他 Bean 实例作为目标,这种关系可由 IoC 容器的依赖注入提供。Spring 默认使用 Java 动态代理来创建 AOP 代理, 这样就可以为任何接口实例创建代理了。当需要代理的类不是代理接口的时候, Spring 自动会切换为使用 CGLIB 代理,也可强制使用 CGLIB。
在Web应用中我们常常将AOP使用在事务管理和日志功能中。
下面是一些关于AOP常用术语的介绍:
- 切面(Aspect) :官方的抽象定义为“一个关注点的模块化,这个关注点可能会横切多个对象”,在本例中,“切面”就是类TestAspect所关注的具体行为,例如,AServiceImpl.barA()的调用就是切面TestAspect所关注的行为之一。“切面”在ApplicationContext中<aop:aspect>来配置。
- 连接点(Joinpoint) :程序执行过程中的某一行为,例如,AServiceImpl.barA()的调用或者BServiceImpl.barB(String _msg, int _type)抛出异常等行为。
- 通知(Advice) :“切面”对于某个“连接点”所产生的动作,例如,TestAspect中对com.spring.service包下所有类的方法进行日志记录的动作就是一个Advice。其中,一个“切面”可以包含多个“Advice”,例如TestAspect
- 切入点(Pointcut) :匹配连接点的断言,在AOP中通知和一个切入点表达式关联。例如,TestAspect中的所有通知所关注的连接点,都由切入点表达式execution(* com.spring.service.*.*(..))来决定
- 目标对象(Target Object) :被一个或者多个切面所通知的对象。例如,AServcieImpl和BServiceImpl,当然在实际运行时,Spring AOP采用代理实现,实际AOP操作的是TargetObject的代理对象。
- AOP代理(AOP Proxy) 在Spring AOP中有两种代理方式,JDK动态代理和CGLIB代理。默认情况下,TargetObject实现了接口时,则采用JDK动态代理,例如,AServiceImpl;反之,采用CGLIB代理,例如,BServiceImpl。强制使用CGLIB代理需要将
<aop:config>
的proxy-target-class
属性设为true
通知(Advice)类型
- 前置通知(Before advice) :在某连接点(JoinPoint)之前执行的通知,但这个通知不能阻止连接点前的执行。ApplicationContext中在<aop:aspect>里面使用<aop:before>元素进行声明。例如,TestAspect中的doBefore方法
- 后通知(After advice) :当某连接点退出的时候执行的通知(不论是正常返回还是异常退出)。ApplicationContext中在<aop:aspect>里面使用<aop:after>元素进行声明。例如,TestAspect中的doAfter方法,所以AOPTest中调用BServiceImpl.barB抛出异常时,doAfter方法仍然执行
- 返回后通知(After return advice) :在某连接点正常完成后执行的通知,不包括抛出异常的情况。ApplicationContext中在<aop:aspect>里面使用<after-returning>元素进行声明。
- 环绕通知(Around advice) :包围一个连接点的通知,类似Web中Servlet规范中的Filter的doFilter方法。可以在方法的调用前后完成自定义的行为,也可以选择不执行。ApplicationContext中在<aop:aspect>里面使用<aop:around>元素进行声明。例如,TestAspect中的doAround方法。
- 抛出异常后通知(After throwing advice) : 在方法抛出异常退出时执行的通知。 ApplicationContext中在<aop:aspect>里面使用<aop:after-throwing>元素进行声明。例如,TestAspect中的doThrowing方法。
切入点表达式
- 通常情况下,表达式中使用”execution“就可以满足大部分的要求。表达式格式如下:
<span style="font-size:14px;"><span style="font-size:12px;"><span style="font-size:10px;">execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?) </span></span></span>
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包下,返回值为任意类型;方法名任意;参数不作限制的所有方法。
- 通知参数
可以通过args来绑定参数,这样就可以在通知(Advice)中访问具体参数了。例如,<aop:aspect>配置如下
<span style="font-size:14px;"><span style="font-size:12px;"><span style="font-size:10px;"><aop:config> <aop:aspect id="TestAspect" ref="aspectBean"> <aop:pointcut id="businessService" expression="execution(* com.spring.service.*.*(String,..)) and args(msg,..)" /> <aop:after pointcut-ref="businessService" method="doAfter"/> </aop:aspect> </aop:config> </span></span></span>
- 访问当前的连接点
任何通知(Advice)方法可以将第一个参数定义为org.aspectj.lang.JoinPoint
类型。JoinPoint
接口提供了一系列有用的方法, 比如getArgs()
(返回方法参数)、getThis()
(返回代理对象)、getTarget()
(返回目标)、getSignature()
(返回正在被通知的方法相关信息)和toString()
(打印出正在被通知的方法的有用信息)。
- Spring Framework源码(五):Spring AOP简介
- Spring Framework源码(六):Spring AOP之解析标签
- Spring Framework源码(七):Spring AOP之
- Spring(五)AOP
- Spring源码阅读(五)—AOP基础
- Spring源码学习笔记(二)-AOP基础简介
- Spring框架 AOP (五)
- Spring详解(五)------AOP
- 初探Spring AOP framework
- Spring AOP + AspectJ framework
- Spring Framework AOP详解
- Spring AOP + AspectJ framework
- Spring framework(5):AOP
- spring的AOP(五)----Spring AOP 编程(AspectJ )
- spring源码剖析(五)利用AOP实现自定义Spring注解
- Spring Framework 开发参考手册 之五 Spring AOP: Spring之面向方面编程
- 理解Spring AOP 原理(三)Spring AOP 源码分析
- Spring AOP简介
- Android开发优化之——从代码角度进行优化
- OEL7.0 上静默安装oracle数据库
- Ubuntu学习之grep
- undefined与null的区别
- Android开发优化之——对界面UI的优化(1)
- Spring Framework源码(五):Spring AOP简介
- Scrapy入门教程
- Android开发优化之——对界面UI的优化(2)
- Android 性能优化之使用MAT分析内存泄露问题
- Android开发优化之——对界面UI的优化(3)
- spring ApplicationContext.xml之PropertyPlaceholderConfigurer配置数据库
- vim中文乱码的解决办法
- c#入门系列——类和对象的代码实现
- WebView、HTML5与PhoneGAP框架——Day01_2