springboot启动慢排查

1.排查每个bean加载时间是否有过长的

@Component
public class BeanInitCostTimeBeanPostProcessor implements BeanPostProcessor {

    private static final Logger logger = LoggerFactory.getLogger(BeanInitCostTimeBeanPostProcessor.class);

    private static final Map<String, Long> TIME_MAP = Maps.newConcurrentMap();

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        TIME_MAP.put(beanName, System.currentTimeMillis());
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (Objects.nonNull(TIME_MAP.get(beanName))) {
            logger.info("beanName {}, init cost {}ms", beanName, System.currentTimeMillis() - TIME_MAP.get(beanName));
        }
        return bean;
    }
}
posted @ 2024-05-21 15:22  SpecialSpeculator  阅读(328)  评论(0编辑  收藏  举报