自定义SpringBoot Starter

自定义SpringBoot Starter

来源https://www.bilibili.com/video/BV16q4y1q71D?spm_id_from=333.999.0.0

创建一个空项目

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

新建一个zhl-spring-boot-starter模块

zhl-spring-boot-starter为普通的maven项目,只需要引入zhl-spring-boot-autoconfigure即可

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>zhl-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>zhl-spring-boot-autoconfigure</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

在这里插入图片描述

新建一个zhl-spring-boot-autoconfigure模块

在这里插入图片描述
在这里插入图片描述

这些都可以删除掉

在这里插入图片描述

在这里插入图片描述

自定义注解

package com.example.annotation;

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

/**
 * @author: zhl
 * @Time: 2022/3/26  23:44
 */
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Log {
    String value() default "";
}

拦截器

package com.example.interceptor;

import com.example.annotation.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

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

/**
 * @author: zhl
 * @Time: 2022/3/26  23:46
 */
@Slf4j
public class LogInterceptor implements HandlerInterceptor {
    private ThreadLocal threadLocal = new ThreadLocal();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            Log methodAnnotation = handlerMethod.getMethodAnnotation(Log.class);
            if (methodAnnotation != null) {
                long start = System.currentTimeMillis();
                threadLocal.set(start);
            }
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            Method method = handlerMethod.getMethod();
            Log methodAnnotation = handlerMethod.getMethodAnnotation(Log.class);
            if (methodAnnotation != null) {
                String requestURI = request.getRequestURI();
                String globalMethodName = method.getDeclaringClass().getName() + "#" + method.getName();
                String desc = methodAnnotation.value();
                long end = System.currentTimeMillis();
                long start = (long) threadLocal.get();
                long dur = end - start;
                threadLocal.remove();
                log.info("请求路径:{},请求方法:{},描述信息:{},总计耗时:{}", requestURI, globalMethodName, desc, dur);
            }
        }
    }
}
package com.example.autoconfigure;

import com.example.interceptor.LogInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author: zhl
 * @Time: 2022/3/26  23:59
 */
@Configuration
public class LogAutoConfigure implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LogInterceptor());
    }
}

resource目录下新建META-INF目录,新建spring.factories文件

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.autoconfigure.LogAutoConfigure

在这里插入图片描述

打开一个SpringBoot项目,引入zhl-spring-boot-starter

<dependency>
    <groupId>org.example</groupId>
    <artifactId>zhl-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>
/**
 * @author: zhl
 * @Time: 2022/3/27  0:10
 */
@RestController
public class LogTest {

    @Log(value = "testLog")
    @GetMapping("/testLog")
    public String testLog(){
        return "success";
    }
}

请求http://localhost:8080/testLog,控制台输出

2022-03-27 09:52:15.858  INFO 10632 --- [nio-8080-exec-4] com.example.interceptor.LogInterceptor   : 请求路径:/testLog,请求方法:com.example.controller.LogTest#testLog,描述信息:testLog,总计耗时:5

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值