若依操作日志记录与aop实现
若依框架中,通过aop记录操作日志,代码如下:
@Before(value = "@annotation(controllerLog)") public void boBefore(JoinPoint joinPoint, Log controllerLog) { TIME_THREADLOCAL.set(System.currentTimeMillis()); }
代码解读
这个aop切面函数用于在标注有 @log 注解的方法执行前,将当前时间记录到 time_threadlocal 中。
流程
在加入 @log 注解的方法被调用时:
- @before 注解会触发 bobefore 方法执行。
- bobefore 方法将当前时间记录到 time_threadlocal 中。
- 被注解方法执行。
- 执行 @afterreturning 或 @afterthrowing 注解的方法,计算操作消耗时间,并记录到数据库中。
回答您的具体问题:
-
首先@annotation里不是要全限定类名的注解吗?
- 可以是全限定类名,但这里是直接使用形参 controllerlog 代表注解类型,全限定类名为 com.ruoyi.common.annotation.log。
-
其次controllerlog又是哪来的,项目中都找不到这个注解啊,而且业务方法上是加的@log啊,为什么不是@annotation("com.xxx.log")?
- controllerlog 是 bobefore 方法的形参,表示带有 @log 注解的方法。
-
最后,idea是怎么分辨出controllerlog是log类的?
- idea 根据 aspectj 源代码的切面表达式解析规则来分辨。