Logger toutes les propriétés de Spring Boot

Classe pour logguer toutes les propriétés avec Spring boot :

 1package com.toto.myapp.util;
 2
 3import org.slf4j.Logger;
 4import org.slf4j.LoggerFactory;
 5import org.springframework.boot.context.event.ApplicationPreparedEvent;
 6import org.springframework.context.ApplicationListener;
 7import org.springframework.core.env.ConfigurableEnvironment;
 8import org.springframework.core.env.EnumerablePropertySource;
 9import org.springframework.core.env.PropertySource;
10
11import java.util.LinkedList;
12import java.util.List;
13
14public class PropertiesLogger implements ApplicationListener<ApplicationPreparedEvent> {
15  private static final Logger log = LoggerFactory.getLogger(PropertiesLogger.class);
16
17  private ConfigurableEnvironment environment;
18  private boolean isFirstRun = true;
19
20  @Override
21  public void onApplicationEvent(ApplicationPreparedEvent event) {
22    if (isFirstRun) {
23      environment = event.getApplicationContext().getEnvironment();
24      printProperties();
25    }
26    isFirstRun = false;
27  }
28
29  public void printProperties() {
30    for (EnumerablePropertySource propertySource : findPropertiesPropertySources()) {
31      log.info("******* " + propertySource.getName() + " *******");
32      String[] propertyNames = propertySource.getPropertyNames();
33      Arrays.sort(propertyNames);
34      for (String propertyName : propertyNames) {
35        String resolvedProperty = environment.getProperty(propertyName);
36        String sourceProperty = propertySource.getProperty(propertyName).toString();
37        if(resolvedProperty.equals(sourceProperty)) {
38          log.info("{}={}", propertyName, resolvedProperty);
39        }else {
40          log.info("{}={} OVERRIDDEN to {}", propertyName, sourceProperty, resolvedProperty);
41        }
42      }
43    }
44  }
45
46  private List<EnumerablePropertySource> findPropertiesPropertySources() {
47    List<EnumerablePropertySource> propertiesPropertySources = new LinkedList<>();
48    for (PropertySource<?> propertySource : environment.getPropertySources()) {
49      if (propertySource instanceof EnumerablePropertySource) {
50        propertiesPropertySources.add((EnumerablePropertySource) propertySource);
51      }
52    }
53    return propertiesPropertySources;
54  }
55}

Classe permettant de lister toutes les propriétés Pour l'utiliser, il faut utiliser : Java public static void main(String[] args) { SpringApplication springApplication = new SpringApplication(MyApplication.class); springApplication.addListeners(new PropertiesLogger()); springApplication.run(args); } ou alors : `Java @SpringBootApplication public class Application extends SpringBootServletInitializer { private static Class applicationClass = Application.class; @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(applicationClass).listeners(myListener()); }

private ApplicationListener< ApplicationStartedEvent> myListener() {
    ....
}

}`

voir : code ou code avec un war