Spring Boot AOP @Pointcut拦截注解的表达式与运算符

项目场景:

这里主要说下Spring Boot AOP中@Pointcut拦截类上面的注解与方法上面的注解,怎么写表达式怎么,还有@Pointcut中使用运算符。


@PointCut 表达式

拦截注解的表达式有3种:@annotation、@within、@target


1、@annotation

匹配有指定注解的方法(注解作用在方法上面)

@annotation(com.test.aop.demo.MyAnnotation)

2、@within

匹配包含某个注解的类(注解作用在类上面)

@within(com.test.aop.demo.MyAnnotation)

3、@target

匹配目标对象有指定注解的类(注解作用在类上面)

@target(com.test.aop.demo.MyAnnotation)

@target 和@within的区别:
        1、@target(注解A):判断被调用的目标对象中是否声明了注解A,如果有,会被拦截;

        2、@within(注解A): 判断被调用的方法所属的类中是否声明了注解A,如果有,会被拦截;

        3、@target关注的是被调用的对象,@within关注的是调用的方法所在的类;


@PointCut中的运算符

 PointCut中可以使用&&、||、! 运算符

同时匹配方法上的和类上的注解 

@Pointcut("@annotation(com.test.aop.demo.MyAnnotation) || @within(com.test.aop.demo.MyAnnotation)")
public void cutController(){
}

 或者

@Pointcut("@annotation(com.test.aop.demo.MyAnnotation)")
public void cutController(){
}

@Pointcut("@within(com.test.aop.demo.MyAnnotation)")
public void cutService(){
}

@Pointcut("cutController() || cutService()")
public void cutAll(){
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

涛哥是个大帅比

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值