Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I print all the configurations loaded by Spring Boot?

I want to print on the screen all the properties loaded from the configuration file. How can I do it? I could not find too much information about that.

That is because I may load configuration files with the the parameter --spring.config.location and I would like to see if I loaded the file correctly or not.

I'm looking for a console solution, something I can print before the process actually starts to do the tasks.

like image 638
Sinjuice Avatar asked Feb 23 '17 10:02

Sinjuice


2 Answers

Could be this of help? I found it on Spring Boot issue tracker.

I paste it for quick reference but keep in mind the original author is sandor-nemeth.

package de.idealo.ecommerce.order.history.config;

import java.util.Arrays;
import java.util.stream.StreamSupport;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.stereotype.Component;

@Component
public class PropertyLogger  {

    private static final Logger LOGGER = LoggerFactory.getLogger(PropertyLogger.class);

    @EventListener
    public void handleContextRefresh(ContextRefreshedEvent event) {
        final Environment env = event.getApplicationContext().getEnvironment();
        LOGGER.info("====== Environment and configuration ======");
        LOGGER.info("Active profiles: {}", Arrays.toString(env.getActiveProfiles()));
        final MutablePropertySources sources = ((AbstractEnvironment) env).getPropertySources();
        StreamSupport.stream(sources.spliterator(), false)
                .filter(ps -> ps instanceof EnumerablePropertySource)
                .map(ps -> ((EnumerablePropertySource) ps).getPropertyNames())
                .flatMap(Arrays::stream)
                .distinct()
                .filter(prop -> !(prop.contains("credentials") || prop.contains("password")))
                .forEach(prop -> LOGGER.info("{}: {}", prop, env.getProperty(prop)));
        LOGGER.info("===========================================");
    }
}
like image 114
danidemi Avatar answered Oct 20 '22 23:10

danidemi


If you use Spring Boot Actuator, you'll get an /env endpoint which shows you that information.

To enable this add the following dependency to your project:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

The output should look like this:

{
  "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"
  }
}

It shows all loaded configuration files, including defaults, system properties, properties loaded through the config service, ... .

like image 34
g00glen00b Avatar answered Oct 20 '22 23:10

g00glen00b