springboot使用AOP基于注解记录 *** 作日志(含源码)

springboot使用AOP基于注解记录 *** 作日志(含源码),第1张

springboot使用AOP基于注解记录 *** 作日志(含源码)

spring框架让java开发者觉得非常简便,其中spring的两个特性IOC和AOP更是让我们的java代码变得十分得简洁,下面分享一个使用注解基于AOP记录 *** 作日志

目的

自定义注解,只要添加了这个注解的请求在被请求时,都会有 *** 作记录入库

思路 1.首先定义一个 *** 作类型的枚举, *** 作包括增,删,改,查
public enum OperationType {

    SELECT("查询"),
    INSERT("插入"),
    UPDATE("更新"),
    DELETE("删除");

    private final String value;


    public String getValue() {
        return value;
    }

    OperationType(String value) {
        this.value = value;
    }
}
2.自定义一个注解,有两个属性分别为title(标题)和type( *** 作类型)两个属性,作用域在方法上
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Operation {

    String title() default "";

    OperationType value() default OperationType.SELECT;
}
3.定义一个 *** 作类型实体,记录 *** 作日志记录,字段包括标题, *** 作类型,方法名,ip,入参
public class OperationRecord implements Serializable {

    private static final long serialVersionUID = 1L;

    
    private String title;

    
    private String type;

    
    private String method;

    
    private String ip;

    
    private Map requestAndValue;


    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }


    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public Map getRequestAndValue() {
        return requestAndValue;
    }

    public void setRequestAndValue(Map requestAndValue) {
        this.requestAndValue = requestAndValue;
    }

    @Override
    public String toString() {
        return "OperationRecord{" +
                "title='" + title + ''' +
                ", type='" + type + ''' +
                ", method='" + method + ''' +
                ", ip='" + ip + ''' +
                ", requestAndValue=" + requestAndValue +
                '}';
    }
}
4.定义切面,入库 *** 作
@Slf4j
@Aspect
@Component
public class OperationAspect {

    private static final String UNKNOWN = "unknown";


    @Resource
    private OperationRecordService operationRecordService;

    
    @Pointcut("@annotation(com.zyy.log.aop.annocation.Operation)")
    public void pointcut() {

    }

    @Around("pointcut()")
    public Object recordOperation(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        Object obj = proceedingJoinPoint.proceed();

        try {
            // 模拟入库
            OperationRecord operationRecord = new OperationRecord();
            operationRecord.setTitle(getTitle(proceedingJoinPoint));
            operationRecord.setMethod(getMethodName(proceedingJoinPoint));
            operationRecord.setType(getOperationType(proceedingJoinPoint).getValue());
            operationRecord.setIp(getIp());
            operationRecord.setRequestAndValue(getNameAndValue(proceedingJoinPoint));
            operationRecordService.insert(operationRecord);
            System.out.println("operationRecord = " + JSONUtil.toJsonStr(operationRecord));

        } catch (Exception e) {
            throw new RuntimeException("入库失败");
        }
        return obj;
    }


    
    private Operation getAnnotation(ProceedingJoinPoint proceedingJoinPoint) {
        return ((MethodSignature) proceedingJoinPoint.getSignature()).getMethod().getAnnotation(Operation.class);
    }

    
    private OperationType getOperationType(ProceedingJoinPoint proceedingJoinPoint) {
        return getAnnotation(proceedingJoinPoint).value();
    }

    
    private String getTitle(ProceedingJoinPoint proceedingJoinPoint) {
        return getAnnotation(proceedingJoinPoint).title();
    }

    
    private String getMethodName(ProceedingJoinPoint proceedingJoinPoint) {
        MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
        return methodSignature.getMethod().getDeclaringClass().getName() + "." + methodSignature.getMethod().getName();
    }

    
    private String getIp() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = Objects.requireNonNull(attributes).getRequest();

        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        String comma = ",";
        String localhost = "127.0.0.1";
        if (ip.contains(comma)) {
            ip = ip.split(",")[0];
        }
        if (localhost.equals(ip)) {
            // 获取本机真正的ip地址
            try {
                ip = InetAddress.getLocalHost().getHostAddress();
            } catch (UnknownHostException e) {
                log.error(e.getMessage(), e);
            }
        }
        return ip;
    }

    
    private Map getNameAndValue(ProceedingJoinPoint joinPoint) {

        final Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        final String[] names = methodSignature.getParameterNames();
        final Object[] args = joinPoint.getArgs();

        if (ArrayUtil.isEmpty(names) || ArrayUtil.isEmpty(args)) {
            return Collections.emptyMap();
        }
        if (names.length != args.length) {
            log.warn("{}方法参数名和参数值数量不一致", methodSignature.getName());
            return Collections.emptyMap();
        }
        Map map = new HashMap<>();
        for (int i = 0; i < names.length; i++) {
            map.put(names[i], args[i]);
        }
        return map;
    }

}
5.定义测试请求,请求中使用Operation注解
@Slf4j
@RestController
public class TestController {

    
    @GetMapping("/test")
    @Operation(title = "查询列表", value = OperationType.SELECT)
    public String test(String who) {
        return who;
    }

}
6.启动项目,输入http://localhost:8080/demo/test?who=jack查看控制台输出 源码

https://gitee.com/onepiecezyy/demo-log-aop

总结

AOP非常强大,应用在很多场景。希望能帮助到大家

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/zaji/5686233.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存