springboot—spring aop 實現系統操作日志記錄存儲到數據庫


原文:https://www.jianshu.com/p/d0bbdf1974bd

 

 

采用方案: 使用spring 的 aop 技術切到自定義注解上,針對不同注解標志進行參數解析,記錄日志
缺點是要針對每個不同的注解標志進行分別取注解標志,獲取參數進行日志記錄輸出

1. 需要引用的依賴

<!--spring切面aop依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> 在application.properties文件里加這樣一條配置 spring.aop.auto=true //這個配置我的例子中沒有加 也正常運行 

2. 創建實體類

public class SysLog implements Serializable { private Long id; private String username; //用戶名 private String operation; //操作 private String method; //方法名 private String params; //參數 private String ip; //ip地址 private Date createDate; //操作時間 //創建getter和setter方法 } 

3. 使用spring 的 aop 技術切到自定義注解上,所以先創建一個自定義注解類

import java.lang.annotation.*; /** * 自定義注解類 */ @Target(ElementType.METHOD) //注解放置的目標位置,METHOD是可注解在方法級別上 @Retention(RetentionPolicy.RUNTIME) //注解在哪個階段執行 @Documented //生成文檔 public @interface MyLog { String value() default ""; } 

4. 創建aop切面實現類

import com.alibaba.fastjson.JSON; import com.qfedu.rongzaiboot.annotation.MyLog; import com.qfedu.rongzaiboot.entity.SysLog; import com.qfedu.rongzaiboot.service.SysLogService; import com.qfedu.rongzaiboot.utils.HttpContextUtils; import com.qfedu.rongzaiboot.utils.IPUtils; import com.qfedu.rongzaiboot.utils.ShiroUtils; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; import java.lang.reflect.Method; import java.util.Date; /** * 系統日志:切面處理類 */ @Aspect @Component public class SysLogAspect { @Autowired private SysLogService sysLogService; //定義切點 @Pointcut //在注解的位置切入代碼 @Pointcut("@annotation( com.qfedu.rongzaiboot.annotation.MyLog)") public void logPoinCut() { } //切面 配置通知 @AfterReturning("logPoinCut()") public void saveSysLog(JoinPoint joinPoint) { System.out.println("切面。。。。。"); //保存日志 SysLog sysLog = new SysLog(); //從切面織入點處通過反射機制獲取織入點處的方法 MethodSignature signature = (MethodSignature) joinPoint.getSignature(); //獲取切入點所在的方法 Method method = signature.getMethod(); //獲取操作 MyLog myLog = method.getAnnotation(MyLog.class); if (myLog != null) { String value = myLog.value(); sysLog.setOperation(value);//保存獲取的操作 } //獲取請求的類名 String className = joinPoint.getTarget().getClass().getName(); //獲取請求的方法名 String methodName = method.getName(); sysLog.setMethod(className + "." + methodName); //請求的參數 Object[] args = joinPoint.getArgs(); //將參數所在的數組轉換成json String params = JSON.toJSONString(args); sysLog.setParams(params); sysLog.setCreateDate(new Date()); //獲取用戶名 sysLog.setUsername(ShiroUtils.getUserEntity().getUsername()); //獲取用戶ip地址 HttpServletRequest request = HttpContextUtils.getHttpServletRequest(); sysLog.setIp(IPUtils.getIpAddr(request)); //調用service保存SysLog實體類到數據庫 sysLogService.save(sysLog); } } 

5. 接下來就可以在需要監控的方法上添加 aop的自定義注解

格式為 @+自定義注解的類名 @MyLog

//例如在contoller類的方法上加注解 @RestController @RequestMapping("/sys/menu") public class SysMenuController extends AbstractController { @Autowired private SysMenuService sysMenuService; @MyLog(value = "刪除菜單記錄") //這里添加了AOP的自定義注解 @PostMapping("/del") public R deleteBatch(@RequestBody Long[] menuIds) { for (Long menuId : menuIds) { if (menuId <= 31) { return R.error("系統菜單,不能刪除"); } } sysMenuService.deleteBatch(menuIds); return R.ok("刪除成功"); } } 

6. 執行上面的方法操作后,會將記錄保存到數據庫

 
 
 轉 https://www.cnblogs.com/shihaiming/p/9565892.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM