(1)切入點 Pointcut
在介紹Pointcut之前,有必要先介紹 Join Point(連接點)概念。
連接點:程序運行中的某個階段點,比如方法的調用、異常的拋出等。比如方法doSome();
Pointcut是JoinPoint的集合,它是程序中需要注入Advice 的位置的集合,指明Advice要在什么樣的條件下才能被觸發。
org.springframework.aop.Pointcut接口用來指定到特定的類和方法。
(2)通知Advice
它是某個連接點所采用的處理邏輯,也就是向連接點注入的代碼。例如:輸出的日志信息 就是一個Advice
(3)Advisor
Advisor是Pointcut和Advice的配置器,它包括Pointcut和Advice,是將Advice注入程序中Pointcut位置的代碼
<aop:aspectj-autoproxy/>
<aop:config proxy-target-class="true">
<aop:pointcut id="servicePointcut"
expression="execution(* com.cpic..*Service.*(..))" />
<aop:advisor pointcut-ref="servicePointcut" advice-ref="txAdvice"
order="3" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" />
<tx:method name="insert*" />
<tx:method name="remove*" />
<tx:method name="save*" />
<tx:method name="update*" />
<tx:method name="delete*" />
<tx:method name="cancel*" />
<tx:method name="trans*" />
<tx:method name="commit*" />
<tx:method name="submit*" />
<tx:method name="issue*" />
<tx:method name="accept*" />
<tx:method name="underwrite*" />
<tx:method name="modify*" />
<tx:method name="calculate*" />
<tx:method name="copy*" />
<tx:method name="print*" />
<tx:method name="create*" />
<tx:method name="send*" />
<tx:method name="activate*" />
<tx:method name="generate*" />
<tx:method name="do*" />
<tx:method name="find*" read-only="true" />
<tx:method name="get*" read-only="true" />
<tx:method name="load*" read-only="true" />
<tx:method name="list*" read-only="true" />
<!-- log方法會啟動一個新事務 -->
<tx:method name="log*" propagation="REQUIRES_NEW"
isolation="READ_COMMITTED" />
<!-- 如果通過java代碼來進行分庫判斷,這里exeNewTS方法需要啟動一個新事務 ,切換數據源時使用-->
<tx:method name="exeNewTS*" propagation="REQUIRES_NEW"
isolation="READ_COMMITTED" />
<!-- <tx:method name="exeNewTS*"/> -->
</tx:attributes>
</tx:advice>