<aop:config> <aop:pointcut id="serviceOperation" expression="execution(* *..service*..*(..))"/> <aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice"/> </aop:config>
<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>
execution
切入點指示符。執行表達式的格式以下:
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)
*
,它表明了匹配任意的返回類型。 一個全限定的類型名將只會匹配返回給定類型的方法。名字模式匹配的是方法名。 你可使用
*
通配符做爲全部或者部分命名模式。 參數模式稍微有點複雜:
()
匹配了一個不接受任何參數的方法, 而
(..)
匹配了一個接受任意數量參數的方法(零或者更多)。 模式
(*)
匹配了一個接受一個任何類型的參數的方法。 模式
(*,String)
匹配了一個接受兩個參數的方法,第一個能夠是任意類型, 第二個則必須是String類型。更多的信息請參閱AspectJ編程指南中
語言語義的部分。
execution(public * *(..))
execution(* set*(..))
AccountService
接口定義的任意方法的執行:
execution(* com.xyz.service.AccountService.*(..))
execution(* com.xyz.service.*.*(..))
execution(* com.xyz.service..*.*(..))
within(com.xyz.service.*)
within(com.xyz.service..*)
AccountService
接口的代理對象的任意鏈接點 (在Spring AOP中只是方法執行):
this(com.xyz.service.AccountService)
AccountService
接口的目標對象的任意鏈接點 (在Spring AOP中只是方法執行):
target(com.xyz.service.AccountService)
Serializable
接口的鏈接點(在Spring AOP中只是方法執行)
args(java.io.Serializable)
execution(* *(java.io.Serializable))
: args版本只有在動態運行時候傳入參數是Serializable時才匹配,而execution版本在方法簽名中聲明只有一個
Serializable
類型的參數時候匹配。
@Transactional
註解的任意鏈接點 (在Spring AOP中只是方法執行)
@target(org.springframework.transaction.annotation.Transactional)
@Transactional
註解的鏈接點 (在Spring AOP中只是方法執行):
@within(org.springframework.transaction.annotation.Transactional)
@Transactional
註解的鏈接點 (在Spring AOP中只是方法執行)
@annotation(org.springframework.transaction.annotation.Transactional)
@Classified
註解的鏈接點(在Spring AOP中只是方法執行)
@args(com.xyz.security.Classified)
tradeService
'的Spring bean之上的鏈接點 (在Spring AOP中只是方法執行):
bean(tradeService)
*Service
'的Spring bean之上的鏈接點 (在Spring AOP中只是方法執行):
bean(*Service)