Spring切入點表達式常用寫法


原文地址   http://lavasoft.blog.51cto.com/62575/172292


  下面給出一個常見是示例:

<aop:config> 
    <aop:pointcut id="serviceOperation" expression="execution(* *..service*..*(..))"/> 
    <aop:advisor pointcut-ref="serviceOperation"  advice-ref="txAdvice"/> 
</aop:config>

表達式所處位置如上pointcut的位置。
配置這個爲了更好控制切面上的事務,下面是一個事物配置的簡單例子:

<tx:advice id="txAdvice" transaction-manager="transactionManager"> 
    <tx:attributes> 
        <tx:method name="delete*" rollback-for="Exception"/> 
        <tx:method name="save*" rollback-for="Exception"/> 
        <tx:method name="update*" rollback-for="Exception"/> 
        <tx:method name="*" read-only="true" rollback-for="Exception"/> 
    </tx:attributes> 
</tx:advice>

通過切面、通知的配置,就爲所有的delete/save/update開頭的方法添加上了一致性事務,對其他方法添加上了只讀事務。
這個還不夠細,如果要寫更爲詳細的控制,就需要研究AspectJ切點配置的語法了,其實研究這些標準,還不如拿幾個例子看看,解決實際問題就行了。就像寫正則表達式一樣,標準明擺着,要寫好卻不容易,從例子着手就能快速上手和領悟。
 
以下文檔來自Spring中文開發指南2.5文檔,由滿江紅開源組織翻譯:
 SpringAOP 

發佈了25 篇原創文章 · 獲贊 9 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章