首先在 maven 中添加 aop 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
Logs 实体类
private String name;//操作人
private String method;//具体路径
private String parameter;//参数
private String consuming;//操作时长
private String content;//内容
private String time;//创建时间
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
public String getJobnum() {
return jobnum;
}
public void setJobnum(String jobnum) {
this.jobnum = jobnum;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getParameter() {
return parameter;
}
public void setParameter(String parameter) {
this.parameter = parameter;
}
public String getConsuming() {
return consuming;
}
public void setConsuming(String consuming) {
this.consuming = consuming;
}
然后创建自定义元注解
package com.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author 作者 : 小布
* @version 创建时间 : 2019年6月10日 上午10:10:52
* @explain 类说明 : 操作日志元注解
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface RecordLog {
String value() default "";
}
创建我们的实现类
import java.lang.reflect.Method;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.ProceedingJoinPoint;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.stereotype.Component;
/**
* @author 作者 : 小布
* @version 创建时间 : 2019年6月10日 上午10:17:06
* @explain 类说明 :
*/
@Aspect
@Component
public class LogAspect {
@Autowired接口
private MasterMapper masterMapper;//引入 mapper 接口 实现添加日志入库
@Pointcut("@annotation(com.annotation.RecordLog)")//自定义元注解
public void pointcut() {
}
@Around("pointcut()")
public Object around(ProceedingJoinPoint point){
Object result = null;
long beginTime = System.currentTimeMillis();
try {
// 执行方法
result = point.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
// 执行时长(毫秒)
long time = System.currentTimeMillis() - beginTime;
//保存日志
saveLog(point, time);
return result;
}
public void saveLog(ProceedingJoinPoint joinPoint, long time){
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
Logs sysLog = new Logs();//对应的日志实体
RecordLog logAnnotation = method.getAnnotation(RecordLog.class);//自定义元注解
if (logAnnotation != null) {
// 注解上的描述
sysLog.setContent(logAnnotation.value());
}
// 请求的方法名
String className = joinPoint.getTarget().getClass().getName();
String methodName = signature.getName();
sysLog.setMethod(className + "." + methodName + "()");
// 请求的方法参数值
Object[] args = joinPoint.getArgs();
// 请求的方法参数名称
LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer();
String[] paramNames = u.getParameterNames(method);
if (args != null && paramNames != null) {
String params = "";
for (int i = 0; i < args.length; i++) {
params += " " + paramNames[i] + ": " + args[i];
}
sysLog.setParameter(params);
}
SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();//获取 shiro对象
sysLog.setName(user.getFullName());//也可以获取 session 等缓存
sysLog.setConsuming((int) time+"");//操作时长 毫秒
masterMapper.isnertLogs(sysLog);//添加日志表
}
}
实现 只需要在方法上加入元注解 @RecordLog(“操作内容”) 即可
最后 数据库 结果