Springboot-starter的自动配置原理-及案例实现6

本文涉及的产品
Serverless 应用引擎 SAE,800核*时 1600GiB*时
性能测试 PTS,5000VUM额度
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
简介: Springboot-starter的自动配置原理-及案例实现6

第二步:自定义MyLog注解

    注解上的两个注解方法作用:

@Target(ElementType.METHOD) //这个注解加在方法上,通过ElementType的枚举指定这个注解加在什么位置
@Retention(RetentionPolicy.RUNTIME) //这个注解运行时生效
————————————————
```package com.laoyang.log;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**

  • 自定义日志注解
    */
    @Target(ElementType.METHOD) //这个注解加在方法上,通过ElementType的枚举指定这个注解加在什么位置
    @Retention(RetentionPolicy.RUNTIME) //这个注解运行时生效
    public @interface MyLog {

    /**

    • 方法描述
      */
      String desc() default "";
      }
      第三步:自定义日志拦截器MyLogInterceptor,用来计算加了MyLog注解后的方法从开始到结束总共的运行时间。 、package com.laoyang.log;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**

  • @author:Kevin
  • @create: 2022-09-17 10:41
  • @Description: 自定义拦截器
    */

public class MylogInterceptor extends HandlerInterceptorAdapter {

private static final ThreadLocal<Long> startTimeThreadLocal = new ThreadLocal<>();
/**
 *      controller方法执行之前
 * @param request
 * @param response
 * @param handler
 * @return
 * @throws Exception
 */
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

    HandlerMethod handlerMethod = (HandlerMethod)handler;  //转换成方法处理器
    Method method = handlerMethod.getMethod();  //获得被拦截的方法对象
    MyLog annotation = method.getAnnotation(MyLog.class);  //获得方法上的MyLog注解

    if (annotation != null){
        //说明当前拦截的方法加入了MyLog注解
        long currentTimeMills = System.currentTimeMillis();
        startTimeThreadLocal.set(currentTimeMills);
    }

    return true;
}

/**
 *      controller方法执行之后
 * @param request
 * @param response
 * @param handler
 * @param modelAndView
 * @throws Exception
 */

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    HandlerMethod handlerMethod = (HandlerMethod)handler;  //转换成方法处理器
    Method method = handlerMethod.getMethod();  //获得被拦截的方法对象
    MyLog annotation = method.getAnnotation(MyLog.class);  //获得方法上的MyLog注解
    if (annotation!=null){
        //说明当前拦截的方法加入了MyLog注解
        long endTime = System.currentTimeMillis();
        Long startTime = startTimeThreadLocal.get();
        long optTime = endTime - startTime; //计算controller方法执行时间

        String requestUri = request.getRequestURI();  //获取当前请求的地址
        String methodName = method.getDeclaringClass().getName() + "." +
                method.getName();    //获取当前请求的方法名称
        String methodDesc = annotation.desc();  //获取注解的声明

        System.out.println("请求uri:" + requestUri);
        System.out.println("请求方法名:" + methodName);
        System.out.println("方法描述:" + methodDesc);
        System.out.println("方法执行时间:" + optTime + "ms");
    }

    super.postHandle(request, response, handler, modelAndView);
}

}
```

相关文章
|
8天前
|
缓存 NoSQL Java
案例 采用Springboot默认的缓存方案Simple在三层架构中完成一个手机验证码生成校验的程序
案例 采用Springboot默认的缓存方案Simple在三层架构中完成一个手机验证码生成校验的程序
57 5
|
6天前
|
消息中间件 Java Maven
深入理解Spring Boot Starter:概念、特点、场景、原理及自定义starter
深入理解Spring Boot Starter:概念、特点、场景、原理及自定义starter
|
8天前
|
JSON 前端开发 Java
Springboot mvc开发之Rest风格及RESTful简化开发案例
Springboot mvc开发之Rest风格及RESTful简化开发案例
16 2
|
8天前
|
SQL Java 数据库连接
2万字实操案例之在Springboot框架下基于注解用Mybatis开发实现基础操作MySQL之预编译SQL主键返回增删改查
2万字实操案例之在Springboot框架下基于注解用Mybatis开发实现基础操作MySQL之预编译SQL主键返回增删改查
16 2
|
13天前
|
Java 应用服务中间件 Spring
解析Spring Boot自动装配的原理与机制
解析Spring Boot自动装配的原理与机制
25 4
|
4天前
|
JSON 安全 Java
Spring Boot与WebFlux的实战案例
Spring Boot与WebFlux的实战案例
|
4天前
|
开发框架 Java 开发者
Spring Boot中的自动装配原理
Spring Boot中的自动装配原理
|
4天前
|
JSON 安全 Java
Spring Boot与WebFlux的实战案例
Spring Boot与WebFlux的实战案例
|
4天前
|
Java
SpringBoot起步依赖原理分析
SpringBoot起步依赖原理分析
|
27天前
|
缓存 Java 开发者
SpringBoot自动装配原理
SpringBoot自动装配原理
21 1

热门文章

最新文章