这两天在写安全框架
但是一直用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