java 切面 注解_spring aop 之 切面注解配置@Jointcut

切面表达式

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值