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.AfterThrowing;
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 LogAspect {
@Autowired
private LogService logService;
/**
* 日志切入点
*/
@Pointcut("@annotation(com.cdls.carp.business.annotation.Log)")
public void logPointCut(){}
@AfterReturning(pointcut = "logPointCut()")
public void doAfter(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
Method method = currentMethod(joinPoint,methodName);
Log log = method.getAnnotation(Log.class);
LogEntity logEntity = this.initLog(log, methodName);
logEntity.setCommit(true);
logService.save(logEntity);
}
@AfterThrowing(pointcut = "logPointCut()", throwing = "e")
public void doAfterThrowing(JoinPoint joinPoint, Throwable e) {
String methodName = joinPoint.getSignature().getName();
Method method = currentMethod(joinPoint,methodName);
Log log = method.getAnnotation(Log.class);
LogEntity logEntity = this.initLog(log, methodName);
logEntity.setCommit(false);
logEntity.setErrorMessage(e + "");
logService.save(logEntity);
}
/**
* 初始化Log对象
* @param log log注解
* @param methodName 方法名
* @return LogEntity对象
*/
private LogEntity initLog(Log log, String methodName) {
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());
return 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();
}
}
成功