在项目中,操作日志至关重要,操作日志记录的是:谁干了什么事;
我们可以利用面向切面编程实现操作日志的打印与记录:
1.首先,添加依赖:
<!--spring aop 依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
2.自定义注解:
package com.qcby.demo0816.anno;
import com.qcby.demo0816.constant.GlobalConstant;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* TODO 自定义日志注解
* @author DB
* <br>CreateDate 2021/9/17 10:26
*/
@Target(ElementType.METHOD) // 该注解可以作用于那些类型元素上:类、方法、字段
@Retention(RetentionPolicy.RUNTIME) // 运行时生效
public @interface SelfLog {
/*
* @Author liu-miss
* @Description //TODO 操作类型
**/
int type() default GlobalConstant.LOG_TYPE_SELECT;
/*
* @Author liu-miss
* @Description //TODO 模块
**/
String module() default "";
/*
* @Author liu-miss
* @Description //TODO 名称参数
**/
String name() default "name";
}
操作类型:增删改查
3.切面:
package com.qcby.demo0816.aspect;
import com.qcby.demo0816.anno.SelfLog;
import com.qcby.demo0816.common.context.QcbyContext;
import com.qcby.demo0816.entity.OperateLog;
import com.qcby.demo0816.entity.User;
import com.qcby.demo0816.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
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 javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.Objects;
/**
* TODO 操作日志切面
*
* @author DB
* <br>CreateDate 2021/9/17 10:48
*/
@Aspect
@Component
@Slf4j
public class SelfLogAspect {
@Autowired
HttpServletRequest request;
/**
* @Author liu-miss
* @Description //TODO 切入点
**/
@Pointcut("@annotation(com.qcby.demo0816.anno.SelfLog)")
public void pointCut(){
}
/**
* @Author liu-miss
* @Description //TODO 前置通知
**/
@Before("pointCut()")
public void testBefore(JoinPoint joinPoint ){
log.info("我是前置通知");
}
@After("pointCut()")
public void after(){
log.info("我是后置通知");
}
@AfterReturning("pointCut()")
public void afterReturning(){
log.info("我是AfterReturning通知");
}
@AfterThrowing("pointCut()")
public void afterThrowing(){
log.info("我是AfterThrowing通知");
}
@Around("pointCut()")
public Object around (ProceedingJoinPoint joinPoint) throws Throwable{
log.info("进入Around通知....前");
//从切面织入点处通过反射机制获取织入点处的方法
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//获取切入点所在的方法
Method method = signature.getMethod();
String token = request.getHeader("token");
Long userId = null;
if (!StringUtil.isEmpty(token)){
User userDb = QcbyContext.getUser(token);
userId = userDb.getId();
}
SelfLog selfLog = method.getAnnotation(SelfLog.class);
log.info("注解selfLog:{}", selfLog);
OperateLog operateLog = new OperateLog(); //这是操作日志实体类对象
operateLog.setCreateTime(LocalDateTime.now());
operateLog.setType(selfLog.type());
operateLog.setUserId(userId);
String nameValue = selfLog.name();
log.info("nameValue的值为:{}",nameValue);
String name = null;
//获取所有参数值
Object[] objects = joinPoint.getArgs();
// 获取是所有参数名字
String[] paramNames = signature.getParameterNames();
for (int i =0;i<paramNames.length;i++){
if (Objects.equals(nameValue,paramNames[i]) && objects[i] != null){
name = objects[i].toString();
}
}
log.info("name的值为:{}",name);
/**
* 进行业务操作
*/
// 执行目标方法
Object r = joinPoint.proceed();
/**
* 目标方法执行完毕之后,执行的业务增强操作
*/
log.info("结束Around通知....后");
return r;
}
}
4.在我们的controller的方法上加上注解:
5.即可记录对应的操作
总结:
自定义注解加在方法上是要知道该方法是什么;
切面类放在了aspect包下,切面中某些语句的作用:
//切入点,该切面与哪个注解相关联
@Pointcut("@annotation(com.qcby.demo0816.anno.SelfLog)")
//前置通知
@Before("pointCut()")
//后置通知
@Before("pointCut()")
//环绕通知
@Around("pointCut()")