spring aop 之 切面註解配置@Jointcut

切面表達式

execution
within
this
target
args
@target
@within
@agrs
@annotation
&&
||
!java

execution

execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?name-pattern(param-pattern)
            throws-pattern?)

?表明能夠不存在spring

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

匹配類型app

within(com.xyz.service.*) service包下的任意方法
within(com.xyz.service..*) service包及子包下的任意方法
this(com.xyz.service.AccountService) AccountService類下的任意方法

this

target

this,target與within相似,this

args

args(java.io.Serializable)

@target

@within

@target,@within差很少,都是類上有某個註解,就匹配任意方法
@within特指當前類的的方法,不包含父類未重寫的或未重載的方法 @target包含父類的方法.net

@within(org.springframework.transaction.annotation.Transactional)
@target(org.springframework.transaction.annotation.Transactional)

@annotation

方法上存在某個註解pwa

@agrs

方法參數上存在某個註解code

@After

若是要帶參數xml

@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 若是要帶參數get

@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
}
相關文章
相關標籤/搜索