您的位置:首页 > 编程语言 > Java开发

spring AOP 学习笔记

2011-05-21 22:51 218 查看

术语理解:

 

cross-cutting concerns:散落在业务中,但又与业务无关的代码片段。

 

|

|识别为Aspect

|

v

 

Aspect:通过整理cross-cutting concerns,需要从中识别出可重用的、功能单一的切面,例如日志/权限检查切面等。

 

|

|============分割线:上面是业务中的概念,下面是代码中的概念==========

|

|实现为Advisor(主要是PointcutAdvisor = Pointcut  +Advise)

|

v

 

Pointcut:触发时机,即做的前提条件,通常需要target/client符合某些情况时才触发Advise。例如target的类名/方法名符合指定正则表达式时触发Advise。Pointcut支持取交集/并集。
+
Advise:触发内容,即做什么。例如日志动作/权限检查动作的具体实现。
|
|
|_ _ _ Joinpoint: Advise的属性,表明做什么的时刻,spring只能在以下情况中任选:target的方法前/后/环绕/抛出异常时。

Spring AOP的核心思想是代理:

client ----透明调用目标----> proxy ----织入目标----> target
                                                            |
                                                            v
                                               改变行为 / 增加行为
                                                      |            |
                                                     v             v
                                   PointcutAdvisor     IntroductionAdvisor

配置方式:

ProxyBean

|
|
|_ _ _ proxyInterfaces:targetInterface

|
|
|_ _ _ target:targetBean
|
|
|_ _ _ interceptorNames:可以填入Advise/ Advisor/ Interceptor
直接填Advise即省略了Pointcut,表示proxyInterfaces中声明的所有方法都要触发Advise。Interceptor和Advisor类似,只不过前者是AOP Alliance制定的标准,后者是spring自己的标准。

PointcutAdvisor(以DefaultPointcutAdvisor为例)

|
|
|_ _ _ pointcut:pointcutBean

|
|
|_ _ _ advise:adviseBean

动态改变了targetInterface中描述的行为,在pointcutBean中指定了触发时机,在adviseBean中指定了触发内容。

IntroductionAdvisor(以DefaultIntroductionAdvisor为例)

|
|
|_ _ _ constructor-arg0:IntroductionBean

|
|
|_ _ _ constructor-arg1:IntroductionInterface

为target动态增加了IntroductionInterface中描述的行为,其具体实现在IntroductionBean中。

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息