分析Spring Boot项目启动原因之 一 打印bean耗时
由于项目比较久远,积累的代码犹如屎山,然后项目启动也特别慢,然后产生想要优化一下启动速度,故写了一个Bean启动时的耗时分析:
package com.tekrally.assetManagement.util;
import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.list.UnmodifiableList;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import java.util.*;
/**
* @Author chengshengwen
* @Date 2023/6/15 11:41
* @PackageName:com.tekrally.assetManagement.util
* @ClassName: BeanInitMetrics
* @Description: TODO 打印bean耗时的基础工具类
* @Version 1.0
*/
@Service
@Slf4j
public class BeanInitMetrics implements BeanPostProcessor {
private final Map<String, Long> stats = new HashMap<>();
//private Map<String,Integer> metrics = new HashMap<>();
private final List<Metric> metrics = new ArrayList<>();
@Data
public static class Metric{
public Metric() {
}
public Metric(String name, Integer value) {
this.name = name;
this.value = value;
this.createDate = new Date();
}
private String name; //bean名称打印
private Integer value; //bean耗时,单位为毫秒
private Date createDate; //bean耗时的创建时间
}
//重写初始化接口
@Override
public Object postProcessBeforeInitialization(@NotNull Object bean, @NotNull String beanName) throws BeansException {
stats.put(beanName, System.currentTimeMillis());
return bean;
}
//后处理后初始化
@Override
public Object postProcessAfterInitialization(@NotNull Object bean, @NotNull String beanName) throws BeansException {
Long start = stats.get(beanName);
if (start != null) {
metrics.add(new Metric(beanName, Math.toIntExact(System.currentTimeMillis() - start)));
}
return bean;
}
//通过后处理后初始化 - 初始化的时间算出bean耗时
public List<Metric> getMetrics() {
metrics.sort((o1, o2) -> {
try {
return o2.getValue() - o1.getValue();
}catch (Exception e){
return 0;
}
});
log.info("metrics {}", JSON.toJSONString(metrics));
return UnmodifiableList.unmodifiableList(metrics); //只读的集合
}
}
controller提供api调用:
package com.tekrally.assetManagement.controller;
import com.tekrally.assetManagement.util.BeanInitMetrics;
import com.tekrally.entity.Feedback;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
/**
* @Author chengshengwen
* @Date 2023/6/15 11:47
* @PackageName:com.tekrally.assetManagement.controller
* @ClassName: BeanInitController
* @Description: TODO 测试加载bean慢的原因的api层
* @Version 1.0
*/
@RestController
@RequestMapping("beanInit")
public class BeanInitController {
@Resource
private BeanInitMetrics beanInitMetrics;
@GetMapping("/getMetrics")
public List<BeanInitMetrics.Metric> getMetrics() {
return beanInitMetrics.getMetrics();
}
}
测试打印(name为bean的名称,value为耗时单位,为毫秒,createDate为创建的时间戳):