基于SpringBoot的操作日志管理(AOP+自定义注解方式)
程序员文章站
2022-07-15 16:58:23
...
1.数据库设计
2.自定义注解编写
import com.cdls.carp.business.enumerate.MethodTypeEnum;
import java.lang.annotation.*;
/**
* @Description: 日志操作注解
* @Author yjw
* @Date 2020/4/3
**/
@Documented
@Target(value = ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Log {
// 执行模块
String module() default "";
// 操作内容
String content() default "";
// 请求路径
String actionUrl() default "";
// 操作类型:1新增,2修改,3删除, 默认为1
MethodTypeEnum type() default MethodTypeEnum.INSERT;
}
3.Spring AOP编写
import com.cdls.carp.business.annotation.Log;
import com.cdls.carp.business.entity.LogEntity;
import com.cdls.carp.business.service.LogService;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.Date;
/**
* @Description: 日志操作切面
* @Author yjw
* @Date 2020/4/3
**/
@Aspect
@Component
public class LogInterceptor {
@Autowired
LogService logService;
/**
* 日志切入点
*/
@Pointcut("@annotation(com.cdls.carp.business.annotation.Log)")
public void logPointCut(){}
@AfterReturning(pointcut = "logPointCut()")
public void doAfter(JoinPoint joinPoint){
/**
* 解析Log注解
*/
String methodName = joinPoint.getSignature().getName();
Method method = currentMethod(joinPoint,methodName);
Log log = method.getAnnotation(Log.class);
LogEntity logEntity = new LogEntity();
logEntity.setDate(new Date());
logEntity.setMethods(methodName);
logEntity.setType(log.type().getCode());
logEntity.setModule(log.module());
logEntity.setContent(log.content());
logEntity.setActionurl(log.actionUrl());
logService.save(logEntity);
}
/**
* 获取当前执行的方法
* @param joinPoint 连接点
* @param methodName 方法名称
* @return 方法
*/
private Method currentMethod(JoinPoint joinPoint, String methodName) {
/**
* 获取目标类的所有方法,找到当前要执行的方法
*/
Method[] methods = joinPoint.getTarget().getClass().getMethods();
Method resultMethod = null;
for (Method method : methods) {
if (method.getName().equals(methodName)) {
resultMethod = method;
break;
}
}
return resultMethod;
}
}
4.编写日志操作类型的枚举
import lombok.Getter;
/**
* @Description: TODO
* @Author yjw
* @Date 2020/4/3
**/
@Getter
public enum MethodTypeEnum {
INSERT(1, "新增"),
UPDATE(2, "修改"),
DELETE(3, "删除"),
SELECT(4, "查询")
;
MethodTypeEnum(int code, String msg) {
this.code = code;
this.msg = msg;
}
/**
* 状态值
*/
private int code;
/**
* 描述信息
*/
private String msg;
}
5.在方法上增加注解
/**
* 查询所有
* @return
*/
@GetMapping("/list")
@ApiOperation(value = "查询所有")
@Log(module = "用户", content = "adsfa", actionUrl = "/a/q/", type = MethodTypeEnum.SELECT)
public CarpResult<List<UserFeedbackEntity>> list() {
List<UserFeedbackEntity> userFeedbackList = this.userFeedbackService.list();
return new CarpResult<List<UserFeedbackEntity>>(ResultEnum.SUCCESS_CODE.getCode(), ResultEnum.SUCCESS_CODE.getMsg(), userFeedbackList);
}
6.测试类测试
import com.cdls.carp.business.annotation.Log;
import com.cdls.carp.business.controller.UserFeedbackController;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @Description: TODO
* @Author yjw
* @Date 2020/4/3
**/
@SpringBootTest(classes = CarpBusinessApplication.class)
@RunWith(SpringRunner.class)
public class CarpTest {
@Autowired
UserFeedbackController userFeedbackController;
@Test
public void list() {
userFeedbackController.list();
}
}
成功
上一篇: 自动化测试框架Appium集成及应用
下一篇: 连续子数组最大和