spring Aop实现日志
生活随笔
收集整理的這篇文章主要介紹了
spring Aop实现日志
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
作用:日志用來記錄項目的動態,以便于項目出問題時排查解決
這里用到spring的兩大核心之一---------->aop
這版講到的日志是作用在方法的,通過aop來指定某些方法以完成在執行方法前后的一些操作,說是記錄日志不如說是講解aop的用法
首先需要熟知幾個重要的注解:
?有兩種用法(目前我發現的兩種)
第一種用execution
@Pointcut("execution(public * cn.zld.springbootwebsockettest.service.ZUserInfoService.*(..))") public void customize() { }Execution的具體參數和用法可自行百度
我上面的用法是將cn.zld.springbootwebsockettest.service.ZUserInfoService包中的所有pulic方法都定義成切點
@Before(value = "customize()") public void methodBefore(JoinPoint joinPoint) {MethodSignature signature = (MethodSignature) joinPoint.getSignature();LogMethod declaredAnnotation =signature.getMethod().getDeclaredAnnotation(LogMethod.class);log.info("執行方法------>{}", declaredAnnotation.value());String[] parameterNames = signature.getParameterNames();log.info("參數--------->{}", Arrays.toString(parameterNames)); }
?
?第二種:直接用自定義注解
先創建一個自定義注解
?再使用@annotation 標識自定義的日志注解的路徑
@Pointcut("@annotation(cn.zld.springbootwebsockettest.annotation.LogMethod)") public void executeLogMethod() {}@Before(value = "executeLogMethod()") public void methodBefore(JoinPoint joinPoint) {MethodSignature signature = (MethodSignature) joinPoint.getSignature();LogMethod declaredAnnotation = signature.getMethod().getDeclaredAnnotation(LogMethod.class);log.info("執行方法------>{}", declaredAnnotation.value());String[] parameterNames = signature.getParameterNames() }最后將@LogMethod("getPage")注解加到想記錄的方法上即可
?
總結
以上是生活随笔為你收集整理的spring Aop实现日志的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 可信数智云,联通云的个性化标签
- 下一篇: 认清学习的本质 - 读《认知天性》