Created
July 11, 2022 22:46
-
-
Save byronaltice/6f1b38a0ed5c0d0cf8f19e9555a662e3 to your computer and use it in GitHub Desktop.
Print all env properties in Spring before starting up
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import lombok.extern.slf4j.Slf4j; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.boot.context.event.ApplicationPreparedEvent; | |
import org.springframework.context.ApplicationListener; | |
import org.springframework.core.env.ConfigurableEnvironment; | |
import org.springframework.core.env.EnumerablePropertySource; | |
import org.springframework.core.env.PropertySource; | |
import java.util.Arrays; | |
import java.util.LinkedList; | |
import java.util.List; | |
@SpringBootApplication | |
public class Application { | |
public static void main(String[] args) { | |
SpringApplication springApplication = new SpringApplication(Application.class); | |
// For development purposes only. DO NOT log system properties in live envs. | |
springApplication.addListeners(new PropertiesLogger()); | |
springApplication.run(args); | |
} | |
@Slf4j | |
private static class PropertiesLogger implements ApplicationListener<ApplicationPreparedEvent> { | |
private ConfigurableEnvironment environment; | |
private boolean isFirstRun = true; | |
@Override | |
public void onApplicationEvent(ApplicationPreparedEvent event) { | |
if (isFirstRun) { | |
environment = event.getApplicationContext().getEnvironment(); | |
printProperties(); | |
} | |
isFirstRun = false; | |
} | |
public void printProperties() { | |
for (EnumerablePropertySource propertySource : findPropertiesPropertySources()) { | |
log.info("******* " + propertySource.getName() + " *******"); | |
String[] propertyNames = propertySource.getPropertyNames(); | |
Arrays.sort(propertyNames); | |
for (String propertyName : propertyNames) { | |
String resolvedProperty = environment.getProperty(propertyName); | |
String sourceProperty = propertySource.getProperty(propertyName).toString(); | |
if(resolvedProperty.equals(sourceProperty)) { | |
log.info("{}={}", propertyName, resolvedProperty); | |
}else { | |
log.info("{}={} OVERRIDDEN to {}", propertyName, sourceProperty, resolvedProperty); | |
} | |
} | |
} | |
} | |
private List<EnumerablePropertySource> findPropertiesPropertySources() { | |
List<EnumerablePropertySource> propertiesPropertySources = new LinkedList<>(); | |
for (PropertySource<?> propertySource : environment.getPropertySources()) { | |
if (propertySource instanceof EnumerablePropertySource) { | |
propertiesPropertySources.add((EnumerablePropertySource) propertySource); | |
} | |
} | |
return propertiesPropertySources; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment