Use java aop elegant save the log

Use java aop elegant save the log

aims

Reached the target by custom annotations, annotation to the class, as the cut-off point aop annotation of the time-consuming, method name, parameters, this method of execution time, logged

demo

  • Custom annotation
package com.jsong.wiki.backend.annotation;

import java.lang.annotation.*;

/**
 * @Author: Jsong
 * @Date: 2020/3/22 20:51
 * @Description:
 */
// 注解信息添加到java文档中
@Documented
// 注解生命周期,表示注解会被保留到什么阶段,可选择编译阶段,类加载阶段,运行阶段
@Retention(RetentionPolicy.RUNTIME)
// 注解作用的位置,ElementType.METHOD 作用在方法上
@Target(ElementType.METHOD)
public @interface LogAnnotation {
    String action() default "";
}

  • aop sliced
package com.jsong.wiki.backend.aop.aspect;

import com.alibaba.fastjson.JSON;
import com.jsong.wiki.backend.annotation.LogAnnotation;
import com.jsong.wiki.backend.entity.LogEntity;
import com.jsong.wiki.backend.service.LogService;
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.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @Author: Jsong
 * @Date: 2020/3/22 20:48
 * @Description:
 */
@Aspect
@Component
public class LogAspect {

    @Autowired
    private LogService logService;

    /***
     * 切点
     * @date 2020/3/22 21:07
     * @author Jsong
     * @param
     * @return void
     */
    @Pointcut("@annotation(com.jsong.wiki.backend.annotation.LogAnnotation)")
    public void logpointCut() {

    }

//    @Before("createTiemPointCut()")
//    public void before(ProceedingJoinPoint proceedingJoinPoint){
//        Object[] args = proceedingJoinPoint.getArgs();
//        for (Object arg : args) {
//
//        }
//    }

    /***
     * 环绕 执行方法前方法后
     * @date 2020/3/22 21:17
     * @author Jsong
     * @param proceedingJoinPoint
     * @return void
     */
    @Around("logpointCut()")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        LogEntity logEntity = new LogEntity();
        // 执行开始时间
        Long beginTime = System.currentTimeMillis();
        proceedingJoinPoint.proceed();
        // 执行时长
        long time = System.currentTimeMillis() - beginTime;
        logEntity.setDuration((int) time);

        MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
        Method method = signature.getMethod();

        LogAnnotation logAnnotation = method.getAnnotation(LogAnnotation.class);
        // 注解上的名字
        String action = logAnnotation.action();
        logEntity.setOperate(action);

        // 类名
        String className = proceedingJoinPoint.getTarget().getClass().getName();
        String methodName = signature.getName();
        logEntity.setMethod(className + "." + methodName);

        Object[] args = proceedingJoinPoint.getArgs();
        String params = null;
        if (args.length > 0) {
            // date对象 变json字符串
//            params = JSON.toJSONString(args);
            params = JSON.toJSONStringWithDateFormat(args, "yyyy-MM-dd HH:mm:ss");
        }
        logEntity.setParams(params);
        logService.saveLog(logEntity);
    }

}

  • Test
    using a custom annotation method, then this method can be time-consuming to perform, such as information about the parameters recorded in the log
 	@PostMapping("/newFolder")
    @LogAnnotation(action = "新增文件夹")
    public DataResult addFolder(@RequestBody FolderEntity folderEntity) {
        folderService.addFolder(folderEntity);
        return DataResult.success();
    }
Published 83 original articles · won praise 21 · views 50000 +

Guess you like

Origin blog.csdn.net/JsongNeu/article/details/105080457