切面表达式
execution
within
this
target
args
@target
@within
@agrs
@annotation
&&
||
!
execution
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?name-pattern(param-pattern)
throws-pattern?)
?代表可以不存在
execution(public * *(..)) 任意方法
execution(* set*(..)) 任意以set开头的方法
execution(* com.xyz.service.AccountService.*(..)) AccountService类下的任意方法
execution(* com.xyz.service.*.*(..)) service包下的任意方法,不包含子包
execution(* com.xyz.service..*.*(..)) service包下的任意方法,包含子包
execution(* com.xyz.service.*.*()) service包下不带参数的方法
execution(* com.xyz.service.*.*(*,String)) service包下带两参数的方法
within
匹配类型
within(com.xyz.service.*) service包下的任意方法
within(com.xyz.service..*) service包及子包下的任意方法
this(com.xyz.service.AccountService) AccountService类下的任意方法
this
target
this,target与within类似,
args
args(java.io.Serializable)
@within
@target,@within差不多,都是类上有某个注解,就匹配任意方法
@within(org.springframework.transaction.annotation.Transactional)
@target(org.springframework.transaction.annotation.Transactional)
@annotation
方法上存在某个注解
@agrs
方法参数上存在某个注解
@After
如果要带参数
@AfterReturning(
pointcut="com.xyz.myapp.SystemArchitecture.dataAccessOperation()",
returning="retVal")
public void doAccessCheck(Object retVal) {
// ...
}
@AfterThrowing
@AfterThrowing(
pointcut="com.xyz.myapp.SystemArchitecture.dataAccessOperation()",
throwing="ex")
public void doRecoveryActions(DataAccessException ex) {
// ...
}
@Around
@Around("com.xyz.myapp.SystemArchitecture.businessService()")
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
// start stopwatch
Object retVal = pjp.proceed();
// stop stopwatch
return retVal;
}
@Befor 如果要带参数
@Pointcut("com.xyz.myapp.SystemArchitecture.dataAccessOperation() && args(account,..)")
private void accountDataAccessOperation(Account account) {}
@Before("accountDataAccessOperation(account)")
public void validateAccount(Account account) {
// ...
}
@Before("com.xyz.lib.Pointcuts.anyPublicMethod() && @annotation(auditable)")
public void audit(Auditable auditable) {
AuditCode code = auditable.value();
// ...
}
@Before(value="com.xyz.lib.Pointcuts.anyPublicMethod() && target(bean) && @annotation(auditable)",
argNames="bean,auditable")
public void audit(JoinPoint jp, Object bean, Auditable auditable) {
AuditCode code = auditable.value();
// ... use code, bean, and jp
}