當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring AOP注解方式实现日志管理
生活随笔
收集整理的這篇文章主要介紹了
Spring AOP注解方式实现日志管理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 自定義注解
- BussLog
- BussLogAspect
前言:使用注解方式實現日志管理,可以使我們的程序變的清晰、簡單,不和很多業務代碼混在一起。
實現思路大致分為四點
- 設計日志表和日志類,編寫日志Dao和Service以及實現
- 自定義注解,注解中加入幾個屬性,屬性可以標識操作的類型(方法是做什么的)
- 編寫切面,切點表達式使用上面的注解直接定位到使用注解的方法,
- 編寫通知,通過定位到方法,獲取上面的注解以及注解的屬性,然后從session中直接獲取或者從數據庫獲取當前登錄用戶的信息,最后根據業務處理一些日志信息之后調用日志Service存儲日志
設計日志表和日志類此步驟將省略可以根據自己的實際情況設計
自定義注解
BussLog
package com.sl.annotation;import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;/*** @author shuliangzhao* @Title: BussLog* @ProjectName spring-boot-learn* @Description: TODO* @date 2019/10/14 20:06*/ @Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface BussLog {/*** 業務的名稱*/String value() default "";/*** 是否將當前日志記錄到數據庫中*/boolean save() default true; }BussLogAspect
package com.sl.aop;import com.alibaba.fastjson.JSON; import com.sl.annotation.BussLog; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.EnableAspectJAutoProxy; import org.springframework.stereotype.Component;import java.lang.reflect.Method; import java.util.LinkedList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern;/*** @author shuliangzhao* @Title: BussLogAspect* @ProjectName spring-boot-learn* @Description: TODO* @date 2019/10/14 20:08*/ @Aspect @Component @EnableAspectJAutoProxy public class BussLogAspect {private static final Logger log = LoggerFactory.getLogger(BussLogAspect.class);@Pointcut(value = "@annotation(com.sl.annotation.BussLog)")public void pointcut() {}@Around("pointcut()")public Object writeLog(ProceedingJoinPoint point) throws Throwable {//先執行業務Object result = point.proceed();try {handle(point);} catch (Exception e) {log.error("日志記錄出錯!", e);}return result;}private void handle(ProceedingJoinPoint point) throws Exception {Method currentMethod = getMethod(point);//獲取操作名稱BussLog annotation = currentMethod.getAnnotation(BussLog.class);boolean save = annotation.save();String bussinessName = parseParams(point.getArgs(), annotation.value());log.info("{} | {} - {} {} - {}", bussinessName);if (!save) {return;}}private Method getMethod(JoinPoint point) throws NoSuchMethodException {Signature sig = point.getSignature();MethodSignature msig = (MethodSignature) sig;Object target = point.getTarget();return target.getClass().getMethod(msig.getName(), msig.getParameterTypes());}public String parseParams(Object[] params, String bussinessName) {if (bussinessName.contains("{") && bussinessName.contains("}")) {List<String> result = match(bussinessName, "(?<=\\{)(\\d+)");for (String s : result) {int index = Integer.parseInt(s);bussinessName = bussinessName.replaceAll("\\{" + index + "}", JSON.toJSONString(params[index - 1]));}}return bussinessName;}private static List<String> match(String str, String regex) {if (null == str) {return null;}Pattern pattern = Pattern.compile(regex);Matcher matcher = pattern.matcher(str);List<String> list = new LinkedList<>();while (matcher.find()) {list.add(matcher.group());}return list;} }總結
以上是生活随笔為你收集整理的Spring AOP注解方式实现日志管理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring AOP相关术语解释及简单使
- 下一篇: gradle idea java ssm