环境pom依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
主类开启AOP
@EnableAspectJAutoProxy
Log
package com.entity;
import java.lang.annotation.*;
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Log {
/** 要执行的操作类型比如:add操作 **/
public String operationType() default "";
/** 要执行的具体操作比如:添加用户 **/
public String operationName() default "";
}
Controller
@GetMapping("selectAllPlusSql/{name}")
@ResponseBody
@Log(operationType = "selectAll", operationName = "通过MyBatis查找")
public R selectAllPlusSql(@PathVariable("name") String name){
List<Warehouse> list = warehouseService.selectAllPlusSql();
TestDemo.logger.info("=selectAllPlusSql");
return R.ok().data("warehouseList",list);
}
AOP
package com.qyc.aop;
import com.entity.Log;
import com.entity.R;
import com.entity.SysLog;
import com.qyc.service.impl.SysLogServiceImpl;
import com.qyc.service.impl.WarehouseServiceImpl;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
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 java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
@Component
@Aspect
public class LogAop { //切面
//切点
@Pointcut("execution(public * com.qyc.controller..*(..))")
public void webPointCut(){
}
@Before("webPointCut()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
// 接收到请求,记录请求内容
// ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
// HttpServletRequest request = attributes.getRequest();
// System.out.println("通过");
// // 记录下请求内容
// System.out.println("URL : " + request.getRequestURL().toString());
// System.out.println("HTTP_METHOD : " + request.getMethod());
// //IP
// System.out.println("IP : " + request.getRemoteAddr());
// //方法
// System.out.println("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
// //参数
// System.out.println("ARGS : " + Arrays.toString(joinPoint.getArgs()));
//
// //获取时间
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// Date d = new Date();
// String str = sdf.format(d);
//
// Signature signature = joinPoint.getSignature();
//
// //Log注解
// MethodSignature methodSignature = (MethodSignature)signature;
// Method method = methodSignature.getMethod();
// String operationType = method.getAnnotation(Log.class).operationType();
// System.out.println("operationType = " + operationType);
// String opertionName = method.getAnnotation(Log.class).operationName();
// System.out.println("opertionName = " + opertionName);
}
@Autowired
private SysLogServiceImpl sysLogService;
@Around("webPointCut()")
public Object doAroundAdvice(ProceedingJoinPoint proceedingJoinPoint){
Long start = System.currentTimeMillis();
try {
SysLog sysLog = new SysLog();
Long end = System.currentTimeMillis();
// 所需时间
sysLog.setUserTime(end-start);
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
// 记录下请求内容
// URL
sysLog.setUrl(request.getRequestURL().toString());
// HTTP_METHOD
sysLog.setHttpMethod(request.getMethod());
//IP
sysLog.setIp(request.getRemoteAddr());
JoinPoint joinPoint = proceedingJoinPoint;
//方法 CLASS_METHOD
sysLog.setClassMethod(joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName());
//参数 ARGS
sysLog.setArgs(Arrays.toString(joinPoint.getArgs()));
//返回值
R ret = (R) proceedingJoinPoint.proceed();
sysLog.setRet(ret.getMessage());
//获取时间
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// Date d = new Date();
// String str = sdf.format(d);
Signature signature = joinPoint.getSignature();
//Log注解
MethodSignature methodSignature = (MethodSignature)signature;
Method method = methodSignature.getMethod();
String operationType = method.getAnnotation(Log.class).operationType();
String opertionName = method.getAnnotation(Log.class).operationName();
sysLog.setOperationType(operationType);
sysLog.setOperationName(opertionName);
int i = sysLogService.insertLog(sysLog);
return obj;
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return null;
}
}
SysLog实体类
package com.entity;
import com.baomidou.mybatisplus.annotation.*;
import java.util.Date;
import java.io.Serializable;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
/**
* <p>
*
* </p>
*
* @author qiangyuecheng
* @since 2021-01-20
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class SysLog implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 日志ID
*/
@TableId(value = "id", type = IdType.ID_WORKER)
private Long id;
/**
* 操作人ID
*/
private Long userId;
/**
* 操作人名称
*/
private String userName;
/**
* 操作人角色
*/
private String userRole;
/**
* 操作类型
*/
private String operationType;
/**
* 操作名称
*/
private String operationName;
/**
* URL
*/
private String url;
/**
* 请求类型
*/
private String httpMethod;
/**
* 请求IP
*/
private String ip;
/**
* 请求方法名
*/
private String classMethod;
/**
* 请求参数
*/
private String args;
/**
* 使用时间(start-end)
*/
private Long userTime;
/**
* 创建时间
*/
@TableField(fill = FieldFill.INSERT)
private Date gmtCreate;
/**
* 修改时间
*/
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date gmtModified;
/**
* 逻辑删除
*/
@TableLogic
@TableField(fill = FieldFill.INSERT)
private Integer deleted;
private String ret;
}