如何打印Spring Boot加载的所有配置?

Sin*_*ice 7 java spring-boot

我想在屏幕上打印从配置文件加载的所有属性.我该怎么做?我找不到太多关于此的信息.

那是因为我可以使用参数--spring.config.location加载配置文件, 我想看看我是否正确加载了文件.

我正在寻找一个控制台解决方案,我可以在流程实际开始执行任务之前打印.

g00*_*00b 9

如果您使用Spring Boot Actuator,您将获得一个向您显示该信息的/env端点.

要启用此功能,请将以下依赖项添加到项目中:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

输出应如下所示:

{
  "profiles": [

  ],
  "bootstrap": {

  },
  "commandLineArgs": {

  },
  "servletContextInitParams": {

  },
  "systemProperties": {
    "jboss.i18n.generate-proxies": "true",
    "java.runtime.name": "Java(TM) SE Runtime Environment",
    "java.protocol.handler.pkgs": "null|org.springframework.boot.loader",
    ...
  },
  "systemEnvironment": {
    "LOCALAPPDATA": "C:\\Windows\\system32\\config\\systemprofile\\AppData\\Local",
    "PROCESSOR_LEVEL": "6",
    "ProgramFiles": "C:\\Program Files",
    "PUBLIC": "C:\\Users\\Public",
    "NUMBER_OF_PROCESSORS": "2",
    "windir": "C:\\Windows",
    ...
  },
  "applicationConfig: [file:.\/application.yml]": {
    "server.port": 11016,
    "server.tomcat.access-log-enabled": true,
    "server.tomcat.access-log-pattern": "%h %l %u %t \"%r\" %>s %b %D",
    "server.tomcat.basedir": ".\/",
    ...
  },
  "applicationConfig: [classpath:\/application.yml]": {
    ...
    "spring.messages.basename": "messages",
    "spring.messages.cache-seconds": -1,
    "spring.messages.encoding": "UTF-8"
  },
  "defaultProperties": {
    "spring.application.name": "bootstrap"
  }
}
Run Code Online (Sandbox Code Playgroud)

它显示所有已加载的配置文件,包括默认值,系统属性,通过配置服务加载的属性,....

  • 很好,我不知道这一点,但我需要一个控制台解决方案,以便在我的流程开始之前查看配置。 (4认同)
  • 同样在这里。端点很好,但需要应用程序启动。我需要一种方法来找出为什么 Spring 没有加载我的文件,并且应用程序没有启动,因为 Spring 没有加载文件。 (2认同)