1. 简介
starter pom就是Spring AutoConfiguration 自动配置,由spring boot提供的基于spring4.X的基于条件来配置bean功能。
2. Spring AutoConfiguration机制
SpringBoot启动时,扫描 classpath 所有Jar中 META-INF/spring.factories文件,读取org.springframework.boot.autoconfigure.EnableAutoConfiguration 指定的Configuration,根据Configuration上的Conditional条件自动创建bean,注入容器
3. 模拟实现
3.1 功能描述
在系统运行中,经常需要记录每个方法执行的时间,以下就是通过定义一个日志需要记录的注解,通过拦截器来判断如果方法上使用了该注解,那么我们记录进入、离开以及在该方法的执行时长的日志记录
3.2 引入jar包
spring boot版本:2.1.0.RELEASE
//starter pom依赖的包
implementation('org.springframework.boot:spring-boot-autoconfigure')
//拦截器依赖的包
implementation('org.springframework.boot:spring-boot-starter-web')
3.3 定义注解
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 自定义日志拦截的注释
*/
//注解运用的地方
@Target(ElementType.METHOD)
//生命周期
@Retention(RetentionPolicy.RUNTIME)
public @interface LoggerAnnotation {
}
3.4 定义拦截器
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;
/**
* @program: start-util
* @description: 日志拦截器
* @author: chengqj
* @create: 2018-11-14 19:56
**/
public class LoggerInterceptor extends HandlerInterceptorAdapter {
private ThreadLocal<Long> loggerEntityThreadLocal = new ThreadLocal<Long>();
private Logger logger = LoggerFactory.getLogger(LoggerInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HandlerMethod handlerMethod = (HandlerMethod) handler;
Method method = handlerMethod.getMethod();
//获取当前方法上的指定注解
LoggerAnnotation loggerAnnotation = method.getAnnotation(LoggerAnnotation.class);
//判断当前注解是否存在
if (loggerAnnotation != null) {
long startTime = System.currentTimeMillis();
loggerEntityThreadLocal.set(startTime);
request.setAttribute("startTime", startTime);
logger.info("进入" + method.getName() + "方法的时间是:" + startTime);
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
HandlerMethod handlerMethod = (HandlerMethod) handler;
Method method = handlerMethod.getMethod();
//获取当前方法上的指定注解
LoggerAnnotation loggerAnnotation = method.getAnnotation(LoggerAnnotation.class);
//判断当前注解是否存在
if (loggerAnnotation != null) {
long endTime = System.currentTimeMillis();
long startTime = loggerEntityThreadLocal.get();
long periodTime = endTime - startTime;
logger.info("离开" + method.getName() + "方法的时间是:" + endTime);
logger.info("在" + method.getName() + "方法的时长是:" + periodTime);
}
loggerEntityThreadLocal.remove();
}
}
3.5 注册拦截器
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;
/**
* @program: start-util
* @description: 拦截器注册
* @author: chengqj
* @create: 2018-11-14 20:00
**/
@Configuration
public class InterceptorRegister implements WebMvcConfigurer {
@Bean
public LoggerInterceptor loggerInterceptor(){
return new LoggerInterceptor();
}
@Override
public void addInterceptors(InterceptorRegistry registry){
registry.addInterceptor(loggerInterceptor());
}
}
3.6 spring.factories
在resources目录下的META-INFO\spring.factories文件中添加
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.chengqj.startutil.InterceptorRegister
3.7 jar包上传到私有仓库
uploadArchives {
repositories {
mavenDeployer {
repository(url: "http://IP:8089/nexus/content/repositories/qxkj/") {
authentication(userName: "admin", password: "admin123")
}
pom.version = "1.1.0"
pom.artifactId = "start-util"
pom.groupId = "com.chengqj"
}
}
}
3.8 使用端引用jar包
compile 'com.chengqj:start-util:1.1.0'
3.9 调用方法添加@LoggerAnnotation注解
3.10 运行后查看日志
进入queryOrder方法的时间是:1542198237056
离开queryOrder方法的时间是:1542198237689
在queryOrder方法的时长是:633