用于解决项目启动慢的问题
@Component
public class LoggerBeanLoadCostPostProcessor implements BeanPostProcessor{
private static Logger logger = LoggerFactory.getLogger(LoggerBeanLoadCostPostProcessor.class);
private static Map<String, Long> cost = new HashMap<>(10000);
private static AtomicInteger beanCount = new AtomicInteger(0);
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
logger.info("first load_spring_bean_cost_info,bean init beanName:{},begintime : {}",
beanName,System.currentTimeMillis());
cost.put(beanName, System.currentTimeMillis());
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
beanCount.incrementAndGet();
if(cost.get(beanName) == null){
logger.warn("first load_spring_bean_cost_info, beanCount : {} ,cost.get(beanName : {} ) is null",beanCount.get(),beanName);
}else{
logger.info("first load_spring_bean_cost_info,bean after beanCount : {},beanName:{},beanType :{} before: {} ,cost : {}",beanCount.get(),beanName,
bean.getClass().getName(),cost.get(beanName), (System.currentTimeMillis()- cost.get(beanName)));
}
return bean;
}
}