当前位置: 首页 > news >正文

springboot 显示打印加载bean耗时工具类

一  spring的原生接口说明

1.1 接口说明

Aware是Spring框架提供的一组特殊接口,可以让Bean从Spring容器中拿到一些资源信息。

BeanFactoryAware:实现该接口,可以访问BeanFactory对象,从而获取Bean在容器中的相关信息。

EnvironmentAware:实现该接口,可以访问Environment对象,从而获取环境相关的配置属性,比如系统属性、环境变量等。

ResourceLoaderAware:实现该接口,可以访问ResourceLoader对象,从而获取资源加载器,用于加载类路径下的资源文件。

MessageSourceAware:实现该接口,可以访问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
这个类实现了Spring框架的BeanPostProcessor接口,用于在bean初始化前后记录每个bean的创建时间成本
* @param
* @return
*/
@Component
public class TimeCostBeanPostProcessor implements BeanPostProcessor {private Map<String, Long> costMap = Maps.newConcurrentMap();private Long costSumTime = 0L;@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {costMap.put(beanName, System.currentTimeMillis());return bean;}@Overridepublic 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.监控事件

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
这个类实现了Spring框架的ApplicationListener<ApplicationEvent>接口,用于监听并处理应用上下文中的事件。
* @param
* @return
*/
@Component
public class ApplicationEventListener implements ApplicationListener<ApplicationEvent> {private static final Logger logger = LoggerFactory.getLogger(ApplicationEventListener.class);@Overridepublic void onApplicationEvent(ApplicationEvent event) {logger.info("=======event received : {}", event.getClass().getName());}
}

测试案例结果:

 

 

http://www.dtcms.com/a/264544.html

相关文章:

  • iOS App无源码安全加固实战:如何对成品IPA实现结构混淆与资源保护
  • python中两种策略模式的实现
  • 2-RuoYi-UI管理平台的启动
  • 大语言模型随意猜测网址引发网络安全危机
  • 零信任安全:重塑网络安全架构的革命性理念
  • 【Unity3D实现加载在线地图——WebGL】
  • 【ABAP】 从无到有 新建一个Webdynpro程序
  • 同一水平的 RISC-V 架构的 MCU,和 ARM 架构的 MCU 相比,运行速度如何?
  • Tomcat log日志解析
  • 【Linux】文件权限以及特殊权限(SUID、SGID)
  • Highcharts 安装使用教程
  • Flutter Widget Preview 功能已合并到 master,提前在体验毛坯的预览支持
  • flutter flutter_vlc_player播放视频设置循环播放失效、初始化后获取不到视频宽高
  • 机器学习:集成学习方法之随机森林(Random Forest)
  • AWS RDS Aurora全局数据库转区域数据库实战指南:无缝迁移零停机
  • Windows VMWare Centos Docker部署Springboot 应用实现文件上传返回文件http链接
  • php上传或者压缩图片后图片出现倒转或者反转的问题
  • Hyper-YOLO: When Visual Object Detection Meets Hypergraph Computation
  • 在Ubuntu上多网卡配置HTTP-HTTPS代理服务器
  • c语言中的函数II
  • 今日学习:音视频领域入门文章参考(待完善)
  • 数据结构:数组(Array)
  • 文心快码答用户问|Comate AI IDE专场
  • 文心4.5开源模型部署实践
  • 使用Vue3实现输入emoji 表情包
  • 阿里云AppFlow AI助手打造智能搜索摘要新体验
  • 基于开源链动2+1模式AI智能名片S2B2C商城小程序的场景零售创新研究
  • 【Unity】MiniGame编辑器小游戏(八)三国华容道【HuarongRoad】
  • Active-Prompt:让AI更智能地学习推理的革命性技术
  • BlenderBot对话机器人大模型Facebook开发