自定义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