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 启动过程中的一个较晚阶段,确保所有初始化步骤都已完成。

相关推荐
你的人类朋友10 小时前
😎 Node.js 应用多阶段构建 Dockerfile 详解
后端·docker·容器
小坏讲微服务10 小时前
Spring Boot整合Redis注解,实战Redis注解使用
spring boot·redis·分布式·后端·spring cloud·微服务·mybatis
xie_pin_an11 小时前
MyBatis-Plus 实战:MPJLambdaWrapper 多表联查用法全解析
java·spring boot·spring·mybatis
一个儒雅随和的男子11 小时前
多级缓存解决方案
spring boot·缓存
橘子海全栈攻城狮11 小时前
【源码+文档+调试讲解】基于Spring Boot的考务管理系统设计与实现 085
java·spring boot·后端·spring
追逐时光者11 小时前
一个基于 .NET 8 + DDD 搭建的模块化微服务框架
后端·.net
William_cl11 小时前
C# ASP.NET MVC 数据验证实战:View 层双保险(Html.ValidationMessageFor + jQuery Validate)
后端·c#·asp.net·mvc
Access开发易登软件12 小时前
Access导出带图表的 HTML 报表:技术实现详解
数据库·后端·html·vba·导出·access
Archy_Wang_112 小时前
ASP.NET Core 应用的零停机部署策略
后端·servlet·asp.net