2016/1/18 10:46:21
1.配置
Spring所有的切面和通知器都必須放在一個內(可以配置包含多個元素),每一個可以包含pointcut,advisor和aspect元素
(它們必須按照這個順序進行相應的聲明)
2.aspect配置
<bean id="zjxAspect" class="com.zjx.aspect.ZjxAspect"></bean>
<bean id="zjxAspectBiz" class="com.zjx.aspect.ZjxAspectBiz"></bean>
<aop:config>
<!-- 這個聲明瞭一個切面類 -->
<aop:aspect id="zjxAspectAOP" ref="zjxAspect"></aop:aspect>
</aop:config>
3.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包及其子包下的所有方法時
以下只有Spring中才能用,還有很多配置,可以查閱spring的官方文檔
- within(com.xyz.service.*)
- within(com.xyz.service..*)within用於匹配製定類型內的方法執行
- this(com.xyz.service.AccountService) this用於匹配當前AOP代理對象類型的執行方法
- taget用於匹配當前目標對象類型的執行方法
配置
<bean id="zjxAspect" class="com.zjx.aspect.ZjxAspect"></bean>
<bean id="zjxAspectBiz" class="com.zjx.aspect.ZjxAspectBiz"></bean>
<aop:config>
<aop:aspect id="zjxAspectAOP" ref="zjxAspect">
<aop:pointcut expression="excution(com.zjx.aspect.*Biz*(..))" id="zjxPointcut"/>
</aop:aspect>
</aop:config>
4.通知的配置
例如,在切入點之前織入before方法
<bean id="zjxAspect" class="com.zjx.aspect.ZjxAspect"></bean>
<bean id="zjxAspectBiz" class="com.zjx.aspect.ZjxAspectBiz"></bean>
<aop:config>
<aop:aspect id="zjxAspectAOP" ref="zjxAspect">
<aop:pointcut expression="excution(com.zjx.aspect.*Biz*(..))" id="zjxPointcut"/>
<aop:before method="before" pointcut-ref="zjxPointcut"/>
</aop:aspect>
</aop:config>
其餘的通知可以參考該配置
5.Introduction
- 簡介允許一個切面聲明一個實現指定接口的通知對象,並且提供了一個接口實現類來代表這些對象
- 由中的元素聲明該元素用於聲明所匹配的類型擁有一個新的parent
6.advisor
- advisor就像一個小的自包含的方面,只有一個advice
- 切面自身通過一個bean表示,並且必須實現某個advice接口,同時,advice也可以很好的利用AspectJ的切入點表達式
- Spring通過配置文件中元素支持advisor實際使用中,大多數情況下他會和transactional advice配合使用
- 爲了定義一個advisor的優先級以便讓advice可以有序,可以使用order屬性來定義advisor順序