赞
踩
两个工程如下:customLog-spring-boot-autoconfigure、customLog-spring-boot-starter。每个空的starter都要依赖autoconfigure,即xxx-starter是一个空的项目,里面只是引入xxx-autoconfigure依赖。
customLog-spring-boot-autoconfigure工程:
<dependencies> <!-- 每个自定义starter都要依赖的基础依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
customLog-spring-boot-autoconfigure工程:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LogAnnotation {
//接口方法上的描述信息
String desc() default "";
}
@Slf4j public class LogInterceptor implements HandlerInterceptor { private static final ThreadLocal th = new ThreadLocal<>(); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { HandlerMethod handlerMethod = (HandlerMethod) handler; LogAnnotation methodAnnotation = handlerMethod.getMethodAnnotation(LogAnnotation.class); if(methodAnnotation != null){ long start = System.currentTimeMillis(); th.set(start); } return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { HandlerMethod handlerMethod = (HandlerMethod) handler; LogAnnotation methodAnnotation = handlerMethod.getMethodAnnotation(LogAnnotation.class); if(methodAnnotation != null){ Method method = handlerMethod.getMethod(); String requestURI = request.getRequestURI(); String methodName = method.getDeclaringClass().getName()+":"+method.getName(); String desc = methodAnnotation.desc(); long end = System.currentTimeMillis(); long start = (long)th.get(); long l = end - start; th.remove(); log.info("请求路径:{},请求方法:{},描述信息:{},总计耗时:{}",requestURI,methodName,desc,l); } } }
@Configuration
public class LogAutoConfiguration implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LogInterceptor()).addPathPatterns("/api/**");
WebMvcConfigurer.super.addInterceptors(registry);
}
}
在resources/META-INF/下建立spring.factories文件,配置自动装配类路径
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.customlog.config.LogAutoConfiguration
分别对customLog-spring-boot-autoconfigure、customLog-spring-boot-starter工程进行install,安装到maven仓库。
customLog-spring-boot-starter 工程:
<dependencies>
<dependency>
<groupId>com.customLog</groupId>
<artifactId>customLog-spring-boot-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
新建项目和TestCustomStarter,如下:
@RestController
@RequestMapping("/api")
public class TestCustomStarter {
@LogAnnotation
@GetMapping("/test")
public String test(){
return "测试成果......";
}
}
启动测试项目,访问http://localhost:8080/api/test,出现以下打印日志,则表示成功。
、
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。