spring aop -- 获取注解值

                       

这两天在写安全框架
 但是一直用method Invocation的话有点特别麻烦,看着不爽
 用注解习惯了
现在一个需求就是

我要拿到这个a方法上的printLog注解的desc值

@PrintLog(desc = "i am a")    public void a(){    }
  
  • 1
  • 2
  • 3
  • 4

经过搜索引擎来一遍之后
 点击看官方文档,各种方式
发现了这种符合我需求的方法

为了省事 我定义了这个注解的方法为切点

@Before(“log()&&@annotation(printLog)”)就实现了我的需求
不得不惊叹aspect的语法
长征路漫漫 我还需努力

//定义一个切点@Pointcut("@annotation(com.wuhulala.studySpring.annotation.PrintLog)")    public void log() {    }    @Before("log()&&@annotation(printLog)")    public void before(JoinPoint joinPoint, PrintLog printLog) {        loggger.debug(printLog.desc() + "[" + joinPoint.getSignature().getName() + " : start.....]");    }
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

那么如果需要两个注解呢。。。。

&&表示与的意思  表示既有printLog注解 又有prinLog2注解 才会进入这个切面
具体可以瞅瞅

@annotation(printLog) 需要与参数列表中的那个参数名称相同 如 PrintLog printLog

@Before("log()&&@annotation(printLog)&&@annotation(printLog2)")    public void before(JoinPoint joinPoint, PrintLog printLog, PrintLog2 printLog2) {        logger.debug("printLog[" + printLog.desc() + "]--printLog2[" + printLog2.desc() + "]");        // logger.debug(printLog.desc() + "[" + joinPoint.getSignature().getName() + " : start.....]");        // logger.debug(printLog2.desc() + "[" + joinPoint.getSignature().getName() + " : start.....]");    }
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

还可以这样 如果需要特别多个 不嫌麻烦的话可以慢慢来。。。
还没发现比较好点的方法。。。
慢慢来。。。

之前aspect了解的太少
看来 aspect 也要纳入深入范围了。。

           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.youkuaiyun.com/jiangjunshow

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值