查看Spring Boot项目所有配置信息的几种方法,包括 Actuator端点、日志输出、代码级获取 等方式,附带详细步骤和示例

以下是查看Spring Boot项目所有配置信息的几种方法,包括 Actuator端点日志输出代码级获取 等方式,附带详细步骤和示例:


1. 使用Spring Boot Actuator

Actuator是Spring Boot提供的监控和管理工具,包含/configprops端点可查看所有配置属性。

步骤
1.1 添加依赖

pom.xml中添加Actuator依赖:

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
1.2 配置暴露端点

application.ymlapplication.properties中配置暴露configprops端点:

yaml 复制代码
management:
  endpoints:
    web:
      exposure:
        include: "configprops,health"  # 暴露configprops和health端点
1.3 访问配置信息

启动应用后,访问:

复制代码
http://localhost:{port}/actuator/configprops

例如:http://localhost:8080/actuator/configprops

输出示例
json 复制代码
{
  "configurations": [
    {
      "name": "spring.http",
      "properties": {
        "encoding.auto": {
          "value": "false",
          "origin": "SpringBootAutoConfiguration"
        },
        "encoding.charset": {
          "value": "UTF-8",
          "origin": "Spring Boot default"
        }
      }
    },
    ...
  ]
}

2. 通过日志输出配置信息

在日志中直接打印所有配置属性。

步骤
2.1 配置日志级别

application.yml中启用配置属性日志:

yaml 复制代码
logging:
  level:
    org.springframework.boot.context.properties: DEBUG
2.2 启动应用

启动应用后,日志中会输出所有配置属性的加载信息,例如:

复制代码
DEBUG 12345 --- [           main] o.s.b.c.p.PropertySourceBootstrapConfiguration : Located property source: [...]
DEBUG 12345 --- [           main] o.s.b.c.p.PropertySourceBootstrapConfiguration : Adding property source: [...]
2.3 查看完整配置

若需更详细的输出,可在启动时添加参数:

bash 复制代码
java -jar your-app.jar --show-config

此参数会输出所有合并后的配置属性(Spring Boot 2.3+支持)。


3. 通过代码获取配置信息

在代码中注入Environment或使用@Value获取配置属性。

3.1 获取所有配置
java 复制代码
import org.springframework.core.env.Environment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConfigController {
    @Autowired
    private Environment env;

    @GetMapping("/all-config")
    public Map<String, Object> getAllProperties() {
        return env.getPropertySources()
                .stream()
                .flatMap(ps -> ps.getPropertyNames().stream()
                        .map(name -> new AbstractMap.SimpleEntry<>(name, ps.getProperty(name))))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    }
}
访问接口

访问:

复制代码
http://localhost:8080/all-config

4. 使用Spring Boot DevTools的/env端点

DevTools提供了/env端点,可查询特定配置属性。

步骤
4.1 添加依赖
xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
</dependency>
4.2 访问端点

访问:

复制代码
http://localhost:8080/actuator/env

或查询特定属性:

复制代码
http://localhost:8080/actuator/env/spring.datasource.url

5. 使用@ConfigurationProperties绑定并打印

将配置属性绑定到Bean并打印。

步骤
5.1 创建配置类
java 复制代码
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "your.prefix")
public class YourConfig {
    private String property1;
    // getters/setters
}
5.2 打印配置
java 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class ConfigPrinter implements CommandLineRunner {
    @Autowired
    private YourConfig config;

    @Override
    public void run(String... args) throws Exception {
        System.out.println("Config Property1: " + config.getProperty1());
    }
}

关键配置对比表格

方法 适用场景 优点 缺点
Actuator /configprops 开发/生产环境监控 直接通过HTTP接口查看所有配置 需配置安全策略(避免暴露敏感信息)
日志输出 调试或启动时快速查看 无侵入性,适合临时调试 需手动解析日志内容
代码获取 需要程序内处理配置信息 灵活控制输出格式 需编写代码
DevTools /env 开发环境快速查询 支持查询单个属性 需依赖DevTools模块
@ConfigurationProperties 需要绑定配置到Bean时 类型安全,符合Spring规范 需针对每个配置前缀编写Bean

注意事项

  1. 安全配置

    • 生产环境需限制Actuator端点访问,例如:

      yaml 复制代码
      management:
        endpoints:
          web:
            exposure:
              include: "health"
          security:
            enabled: true
  2. 敏感信息过滤

    • 避免暴露敏感配置(如密码),可通过management.endpoints.web.cors.allowed-origins或安全策略控制访问。
  3. 性能影响

    • /configprops端点在配置复杂时可能返回大量数据,需注意性能。

完整示例代码

application.yml
yaml 复制代码
spring:
  application:
    name: config-demo
  datasource:
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: root

management:
  endpoints:
    web:
      exposure:
        include: "configprops,health"
pom.xml依赖
xml 复制代码
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

通过上述方法,可根据需求选择最适合的配置查看方式。如需进一步优化或解决特定问题(如安全配置、日志过滤),可提供具体场景!

相关推荐
骄马之死2 小时前
SpringMVC + SpringBoot 核心知识点总结
java·spring boot·后端
GoGeekBaird3 小时前
Anthropic技能"(Skills)的经验分享
后端
王码码20353 小时前
多台服务器怎么统一看状态?Beszel 轻量监控,搭起来不费事
运维·服务器·后端·安全·阿里云·接口·web
郑洁文3 小时前
基于Spring Boot的流浪动物救助网站
java·spring boot·后端·毕设·流浪动物救助
螺丝钉code4 小时前
JAVA项目 Claude code CLAUDE.md 到底应该怎么写
java·人工智能·claude code
指令集梦境5 小时前
Cursor + Spring Boot实战:从零写一个RESTful API
spring boot·后端·restful
摇滚侠5 小时前
Maven 入门+高深 单一架构案例 54-59
java·架构·maven·intellij-idea
VidDown5 小时前
Webhook 调试器:让第三方回调“原形毕露”
java·开发语言·javascript·编辑器·postman
码云之上5 小时前
聊聊如何设计一个高效、稳定的 Node.js 接入层
前端·后端·node.js
折哥的程序人生 · 物流技术专研5 小时前
Java 23 种设计模式:从踩坑到精通 | 原型模式 —— 克隆对象,深拷贝与浅拷贝的坑你踩过吗?
java·设计模式·架构·原型模式·单一职责原则