Spring监听bean加载时间

Spring监听bean加载时间,第1张

@Component
public class TimeCostBeanPostProcessor implements BeanPostProcessor {

    Map costMap = Maps.newConcurrentMap();

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

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        Long start = costMap.get(beanName);
        long cost = System.currentTimeMillis() - start;
        if (cost > 0) {
            costMap.put(beanName, cost);
            System.out.println("class: " + bean.getClass().getName()
                               + "\tbean: "+ beanName
                               + "\ttime: "+ cost);
        }
        return bean;
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/723999.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-26
下一篇 2022-04-26

发表评论

登录后才能评论

评论列表(0条)

保存