Redis AOP 实现先找缓存再找数据库(自定义注解+AOP)

环绕通知=前置+目标方法执行+后置通知

业务背景:需要先去redis操作数据,再判断要不要去关系型数据库操作数据

先自定义一个注解(如果有其他需求可以自己加参数),用来做标识以及环绕通知的切入点。

 

    @Around注解的使用

    /*一、
    可自定义切点位置,针对不同切点,方法上的@Around()可以这样写ex:@Around(value = "methodPointcut() && args(..)")
    @Pointcut(value = "@annotation(com.rq.aop.common.annotation.MyAnnotation)")
    public void methodPointcut(){}

    @Pointcut(value = "@annotation(com.rq.aop.common.annotation.MyAnnotation2)")
    public void methodPointcut2(){}

    */

     /*二、

        使用自定义注解

      @Around(value = "@annotation(redisCacheAnnotation)")

               此注解要与下方法参数中的注解参数名名字相同

     */

ProceedingJoinPoint的使用

MethodSignature methodSignature =(methodSignature )joinPoint.getSignature()

获取增强方法方法名: proceedingjoinPoint.getSignature().getName()

获取增强方法上的注解:methodSignature.getMethod().getAnnotation(xxx.class)

执行增强方法:proceedingJoinPoint.proceed()

小结:

        环绕通知是AOP方法里面最常用也是最强大的方法,配合ProceedingJoinPoint可以在增强类里面实现任何你想实现的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值