配置切面aspect
<aop:config>
<aop:aspect id="moocAspectAOP" ref="moocAspect">
</aop:aspect>
</aop:config>
配置pointcut
name-pattern:指定方法名,*代表所以,set*,代表以set开头的所有方法.
parameters pattern:指定方法参数(声明的类型),(..)代表所有参数,(*)代表一个参数,(*,String)代表第一个参数为任何值,第二个为String类型.
Aspectj和Spring AOP都支持的:
1.execution(public **(..)) 切入点为执行public方法时
2.execution(* set*(..))切入点为执行所有set开始的方法时
3.execution(* com.xyz.service.AccountService.*(..))切入点为执行AccountService类中的所有方法时 AccountService 接口的任意方法的执行:
4.execution(* com.xyz.service..(..))切入点为执行com.xyz.service包下的所有方法时
5.execution(* com.xyz.service...(..))切入点为执行com.xyz.service包及其子包下的所有方法时
Spring AOP才支持的:
within(com.xyz.service.*)类中
within(com.xyz.service..*) 包中
within用于匹配指定类型内的方法执行
this(com.xyz.service.AccountService)
this用于匹配当前AOP代理对象类型的执行方法
target(com.xyz.service.AccountService)
target用于匹配当前目标对象类型的执行方法
args(java.io.Serializable)
args用于匹配当前执行的方法传入的参数为指定类型的执行方法
带有@Transactional标注的所有类的任意方法.
@within(org.springframework.transaction.annotation.Transactional)
@target(org.springframework.transaction.annotation.Transactional)
带有@Transactional标注的任意方法.
@annotation(org.springframework.transaction.annotation.Transactional)
***> @within和@target针对类的注解,@annotation是针对方法的注解
参数带有@Transactional标注的方法.
@args(org.springframework.transaction.annotation.Transactional)
参数为String类型(运行是决定)的方法.
args(String)
切入点配置范例
<aop:config>
<aop:aspect id="moocAspectAOP" ref="moocAspect">
<aop:pointcut expression="execution(* com.imooc.aop.schema.advice.biz.*Biz.*(..))" id="moocPiontcut"/>
</aop:aspect>
</aop:config>