自定义注解

37 阅读1分钟
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>
package swagger2.demo.config.my;

import java.lang.annotation.*;

/**
 * @author 21029045
 * Created on 2022/10/14
 * @Description
 */

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
public @interface MyLog {

    /**
     * 接口描述
     **/
    String desc() default "";

}
package swagger2.demo.config.my;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 21029045
 * Created on 2022/10/14
 * @Description
 */
@Aspect
@Component
@Slf4j
public class MyAspect {

    /**
     * @Aspect:标识切面
     * @Pointcut:设置切点,这里以自定义注解为切点,定义切点有很多其它种方式,自定义注解是比较常用的一种。
     * @Before:在切点之前织入,打印了一些入参信息
     * @Around:环绕切点,打印返回参数和接口执行时间
     **/

    @Pointcut("@annotation(swagger2.demo.config.my.MyLog)")
    public void myLog() {
    }


    @Before("myLog()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
    
   
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        MyLog annotation = signature.getMethod().getAnnotation(MyLog.class);
        String desc = annotation.desc();
        log.info("接口描述是===[{}]",desc);

    
        // 开始打印请求日志
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        HttpServletRequest request = requestAttributes.getRequest();

        // 打印请求相关参数

        log.info("======================请求开始========================");
        log.info("url====[{}]", request.getRequestURL().toString());
        log.info("method=====[{}]", request.getMethod());
        // 打印调用 controller 的全路径以及执行方法
        log.info("全路径======[{}]以及执行方法====[{}]", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName());

        log.info("ip====[{}]", request.getRemoteAddr());

        // 打印请求入参
        log.info("入参==[{}]",new ObjectMapper().writeValueAsString(joinPoint.getArgs()));

    }

    @After("myLog()")
    public void doAfter() throws Throwable {
        // 结束后打个分隔线,方便查看
        log.info("===================请求结束===============");
    }

    /**
     * 环绕
     */
    @Around("myLog()")
    public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {

        //开始时间
        long startTime = System.currentTimeMillis();

        Object proceed = proceedingJoinPoint.proceed();

        // 打印出参
        log.info("输出参数==[{}]",new ObjectMapper().writeValueAsString(proceed));

        // 执行耗时
        log.info("执行耗时====》[{}]",System.currentTimeMillis()-startTime);

        return proceed;
    }

}
package swagger2.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import swagger2.demo.config.my.MyLog;

/**
 * @author 21029045
 * Created on 2022/10/14
 * @Description
 */
@RestController
public class tsetx {

    @GetMapping("/get")
    @MyLog(desc = "请求接口")
    public String get() throws InterruptedException {
        Thread.sleep(2000);
        return "hello";
    }
}

image.png