1. pom添加依賴包
<!--添加aop依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
2.配置文件application.yml添加
spring:
aop:
auto: true
3 創建實體類
package com.spring4all.entity; import java.io.Serializable; import java.util.Date; /** * @author shafei * @version 1.0 * @date 10:28 2019/9/7 * @fun */ public class SysLogDO implements Serializable { private Long id; private String username; //用戶名 private String operation; //操作 private String method; //方法名 private String params; //參數 private String ip; //ip地址 private Date createDate; //操作時間 public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getOperation() { return operation; } public void setOperation(String operation) { this.operation = operation; } public String getMethod() { return method; } public void setMethod(String method) { this.method = method; } public String getParams() { return params; } public void setParams(String params) { this.params = params; } public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } }
4.使用spring 的 aop 技術切到自定義注解上,所以先創建一個自定義注解類
package com.spring4all.config.intercepors; import java.lang.annotation.*; /** * @author shafei * @version 1.0 * @date 10:06 2019/9/7 */ @Target(ElementType.METHOD) //注解放置的目標位置,METHOD是可注解在方法級別上
@Retention(RetentionPolicy.RUNTIME) //注解在哪個階段執行
@Documented //生成文檔
public @interface MyLog { String value() default ""; }
5.AOP實現:
package com.spring4all.config; import com.alibaba.fastjson.JSON; import com.spring4all.config.intercepors.MyLog; import com.spring4all.entity.SysLogDO; import com.spring4all.service.SysLogService; 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; /** * @author shafei * @version 1.0 * @date 10:08 2019/9/7 * fun: */ @Aspect @Component public class SysLogAspect { @Autowired private SysLogService sysLogService; //定義切點 @Pointcut //在注解的位置切入代碼 @Pointcut("@annotation( com.spring4all.config.intercepors.MyLog)") public void logPoinCut() { } //切面 配置通知 @AfterReturning("logPoinCut()") public void saveSysLog(JoinPoint joinPoint) { System.out.println("切面。。。。。"); //保存日志 SysLogDO sysLog = new SysLogDO(); //從切面織入點處通過反射機制獲取織入點處的方法 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("shafei"); //獲取用戶ip地址 // HttpServletRequest request = HttpContextUtils.getHttpServletRequest(); sysLog.setIp("192.168.3.11"); //調用service保存SysLog實體類到數據庫 // sysLogService.save(sysLog); System.out.println("將日志記錄到數據庫"); } }
6.接下來就可以在需要監控的方法上添加 aop的自定義注解
格式為 @+自定義注解的類名
@Controller @RequestMapping("/test") public class UserController { @MyLog(value = "測試一下") @GetMapping("/test") @ResponseBody public String test(){ return "test"; } }
說明:
記錄日志的服務層接口SysLogService需要另外寫,這個應該都會寫;
參考https://www.jianshu.com/p/d0bbdf1974bd