Spring Boot 应用启动时打印配置类信息

因为项目中有些配置是在环境变量中指定的,所以想在启动时打印一下实际的配置参数,以方便确认配置是否正确。

下面是查到的比较优雅的写法:通过监听 ApplicationReadyEvent 事件,在事件触发时打印配置参数

java 复制代码
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@RequiredArgsConstructor
public class MyApplicationListener {

    private final MyProperties myProperties;

    @EventListener(ApplicationReadyEvent.class)
    public void handleApplicationReadyEvent() {
        // 打印配置参数
        log.info("我的配置 {}", myProperties);
    }
}

其中 MyProperties 配置类上加了 Lombok 的 @Data 注解,会自动生成 toString 方法。

ApplicationReadyEvent 事件在 ApplicationContext 被初始化和刷新后,且应用程序已经准备好处理请求时触发。这是 Spring Boot 启动过程中的一个较晚阶段,确保所有初始化步骤都已完成。

相关推荐
嘻哈baby12 分钟前
Go context详解:超时控制与请求链路追踪
后端
计算机学姐31 分钟前
基于SpringBoot的智能家教服务平台【2026最新】
java·spring boot·后端·mysql·spring·java-ee·intellij-idea
思成Codes35 分钟前
Go 语言中数组与切片的本质区别
开发语言·后端·golang
ZLZQ_Yuan1 小时前
Spring Boot JPA
spring boot
superman超哥1 小时前
Rust Trait约束(Trait Bounds):类型能力的精确契约
开发语言·后端·rust·rust trait约束·trait bounds·类型能力·精确契约
superman超哥1 小时前
Rust Where子句的语法:复杂约束的优雅表达
开发语言·后端·rust·rust where子句·复杂约束·优雅表达
xiaok1 小时前
使用docker部署koa+sql server+react项目完整过程
后端
SadSunset1 小时前
(44)Spring6集成MyBatis3.5(了解即可,大部分用springboot)
java·spring boot·后端
武子康1 小时前
大数据-199 决策树模型详解:节点结构、条件概率视角与香农熵计算
大数据·后端·机器学习
IT_陈寒1 小时前
Python 3.12 性能优化:5 个鲜为人知但提升显著的技巧让你的代码快如闪电
前端·人工智能·后端