原文:https://www.jianshu.com/p/d0bbdf1974bd
代碼內容為我自己的,並且把依賴的包也錄入進來。
一、引入aop依賴
<!--aop-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
二、創建一個實體類
package com.sj56.breakfast.entity;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
@Data
public class UserLog implements Serializable {
//id
private long id;
//操作用戶用戶名
private String userName;
private long userId;
//用戶操作
private String operation;
//方法名
private String method;
//ip
private String ip;
//操作時間
private Date createTime;
}
三、自定義一個注解
package com.sj56.breakfast.config.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface UserOperation {
String value() default "";
}
四、創建AOP切面類
package com.sj56.breakfast.config.aspect;
import com.sj56.breakfast.config.annotation.UserOperation;
import com.sj56.breakfast.entity.UserLog;
import com.sj56.breakfast.service.userlog.UserLogService;
import org.aopalliance.intercept.Joinpoint;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.spi.http.HttpContext;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
@Aspect
@Component
public class UserLogAspect {
@Autowired
private UserLogService userLogService;
@Autowired
private UserLog userLog;
//在注解的位置切入代碼
@Pointcut("@annotation( com.sj56.breakfast.config.annotation.UserOperation )")
public void loginPointCut(){
}
@After("loginPointCut()")
public void logSave(JoinPoint pjp){
System.out.println("保存日志切面~~~~");
//獲取方法
MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
Method method = methodSignature.getMethod();
userLog.setMethod(method.getName());
//獲取操作
UserOperation userOperation = method.getAnnotation(UserOperation.class);
if (userOperation != null){
String value = userOperation.value();
userLog.setOperation(value);
}
//獲取ip
ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = sra.getRequest();
String ip = request.getRemoteAddr();
userLog.setIp(ip);
//獲取用戶
String userId = request.getParameter("userId");
System.out.println("用戶id:"+userId);
if (userId == null || userId == ""){
userId = "0";
}
//可通過用戶id查詢用戶name
userLog.setUserId(Long.valueOf(userId));
//操作時間
userLog.setCreateTime(new Date());
//保存日志
userLogService.saveLog(userLog);
}
}
五、攔截實例
//添加一個店鋪
@UserOperation(value = "保存一個店鋪")
@RequestMapping("/save")
public BFResponse shopSave(Shop shop, @RequestParam HashMap map){
System.out.println("保存店鋪map-->:"+map);
shopService.shopSave(shop);
return BFResponseUtils.success();
}
數據困user_name列為了方便隨便設置了一個字段插入