<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)