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.
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("===========================================");
}
}
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, ... .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With