springboot 顯示打印加載bean耗時工具類的案例
一 spring的原生接口說明
1.1 接口說明
Aware是Spring框架提供的一組特殊接口,可以讓Bean從Spring容器中拿到一些資源信息。

BeanFactoryAware:實現(xiàn)該接口,可以訪問BeanFactory對象,從而獲取Bean在容器中的相關(guān)信息。
EnvironmentAware:實現(xiàn)該接口,可以訪問Environment對象,從而獲取環(huán)境相關(guān)的配置屬性,比如系統(tǒng)屬性、環(huán)境變量等。
ResourceLoaderAware:實現(xiàn)該接口,可以訪問ResourceLoader對象,從而獲取資源加載器,用于加載類路徑下的資源文件。
MessageSourceAware:實現(xiàn)該接口,可以訪問MessageSource對象,從而獲取國際化消息。
1.2 案例說明
1.打印耗時
package com.ljf.springboot.mybaits.demos.config;
/**
* @ClassName: TimeCostBeanPostProcessor
* @Description: TODO
* @Author: admin
* @Date: 2025/06/29 17:48:32
* @Version: V1.0
**/
import com.google.common.collect.Maps;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* @author admin
* @description
這個類實現(xiàn)了Spring框架的BeanPostProcessor接口,用于在bean初始化前后記錄每個bean的創(chuàng)建時間成本
* @param
* @return
*/
@Component
public class TimeCostBeanPostProcessor implements BeanPostProcessor {
private Map<String, Long> costMap = Maps.newConcurrentMap();
private Long costSumTime = 0L;
@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 {
if (costMap.containsKey(beanName)) {
Long start = costMap.get(beanName);
long cost = System.currentTimeMillis() - start;
if (cost > 0) {
costMap.put(beanName, cost);
System.out.println("bean: " + beanName + "\ttime: " + cost);
}
}
return bean;
}
}2.監(jiān)控事件
package com.ljf.springboot.mybaits.demos.config;
/**
* @ClassName: ApplicationEventListener
* @Description: TODO
* @Author: admin
* @Date: 2025/06/29 17:44:41
* @Version: V1.0
**/
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
/**
* @author admin
* @description
這個類實現(xiàn)了Spring框架的ApplicationListener<ApplicationEvent>接口,用于監(jiān)聽并處理應(yīng)用上下文中的事件。
* @param
* @return
*/
@Component
public class ApplicationEventListener implements ApplicationListener<ApplicationEvent> {
private static final Logger logger = LoggerFactory.getLogger(ApplicationEventListener.class);
@Override
public void onApplicationEvent(ApplicationEvent event) {
logger.info("=======event received : {}", event.getClass().getName());
}
}測試案例結(jié)果:

到此這篇關(guān)于springboot 顯示打印加載bean耗時工具類的文章就介紹到這了,更多相關(guān)springboot 打印加載bean內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java用靜態(tài)工廠代替構(gòu)造函數(shù)使用方法和優(yōu)缺點
這篇文章主要介紹了java用靜態(tài)工廠代替構(gòu)造函數(shù)使用方法和優(yōu)缺點,需要的朋友可以參考下2014-02-02
SpringBoot定時任務(wù)多線程實現(xiàn)示例
在真實的Java開發(fā)環(huán)境中,我們經(jīng)常會需要用到定時任務(wù)來幫助我們完成一些特殊的任務(wù),本文主要介紹了SpringBoot定時任務(wù)多線程實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12
Mybatis plus結(jié)合springboot使用
本文主要介紹了MyBatisPlus使用SpringBoot數(shù)據(jù)庫操作,從添加依賴到測試,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-11-11
spring實現(xiàn)靜態(tài)注入(類或者屬性)操作示例
這篇文章主要為大家介紹了spring實現(xiàn)靜態(tài)注入(類或者屬性)操作示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07
MyBatis利用MyCat實現(xiàn)多租戶的簡單思路分享
這篇文章主要給大家介紹了關(guān)于MyBatis利用MyCat實現(xiàn)多租戶的簡單思路的相關(guān)資料,文中的多租戶是基于多數(shù)據(jù)庫進行實現(xiàn)的,數(shù)據(jù)是通過不同數(shù)據(jù)庫進行隔離,需要的朋友可以參考借鑒,下面來一起看看吧。2017-06-06

