spring aop——切面表达式

本文详细解析了AOP(面向切面编程)中的切点表达式使用方法,包括包/类型匹配、参数匹配、匹配注解等关键概念,通过具体示例展示了如何精确控制切面的介入点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 包/类型匹配:within
//匹配ProductService类里头的所有方法
@Pointcut("within(com.abc.service.ProductService)")

//匹配com.imooc包及子包下所有类的方法
@Pointcut("within(com.abc..*)")
  • 匹配对象:this, target, bean
//匹配AOP对象的目标对象为指定类型的方法,即LogService的aop代理对象的方法
@Pointcut("this(com.abc.log.Loggable)")

//匹配实现Loggable接口的目标对象(而不是aop代理后的对象)的方法
@Pointcut("target(com.abc.log.Loggable)")
//this 可以拦截 DeclareParents(Introduction)
//target 不拦截 DeclareParents(Introduction)

//匹配所有以Service结尾的bean里头的方法
@Pointcut("bean(*Service)")
  • 参数匹配:args
 //匹配任何以find开头而且只有一个Long参数的方法
 @Pointcut("execution(* *..find*(Long))")

 //匹配任何以find开头的而且第一个参数为Long型的方法
 @Pointcut("execution(* *..find*(Long,..))")

 //匹配任何只有一个Long参数的方法
 @Pointcut("within(com.abc..*) && args(Long)")

 //匹配第一个参数为Long型的方法
 @Pointcut("within(com.abc..*) && args(Long,..)")

 

  • 匹配注解:execution
 //匹配任何公共方法
 @Pointcut("execution(public * com.abc.service.*.*(..))")

 //匹配com.imooc包及子包下Service类中无参方法
 @Pointcut("execution(* com.abc..*Service.*())")

 //匹配com.imooc包及子包下Service类中的任何只有一个参数的方法
 @Pointcut("execution(* com.abc..*Service.*(*))")

 //匹配com.imooc包及子包下任何类的任何方法
 @Pointcut("execution(* com.abc..*.*(..))")

 //匹配com.imooc包及子包下返回值为String的任何方法
 @Pointcut("execution(String com.abc..*.*(..))")

 //匹配异常
 execution(public * com.abc.service.*.*(..) throws java.lang.IllegalAccessException)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值