0

Can Anyone explain me what is happening in the below code?

  1. What is the use of MapPropertySource?
  2. What other options instead of MapPropertySource source and applicationConfig?
  3. Why are we filtering in foreach?
    public void handleContextRefreshed(ContextRefreshedEvent event) {
        printActiveProperties((ConfigurableEnvironment) event.getApplicationContext().getEnvironment());
    }

    private void printActiveProperties(ConfigurableEnvironment env) {

        System.out.println("************************* ACTIVE APP PROPERTIES ******************************");

        List<MapPropertySource> propertySources = new ArrayList<>();

        env.getPropertySources().forEach(it -> {
            if (it instanceof MapPropertySource && it.getName().contains("applicationConfig")) {
                propertySources.add((MapPropertySource) it);
            }
        });

        propertySources.stream()
                .map(propertySource -> propertySource.getSource().keySet())
                .flatMap(Collection::stream)
                .distinct()
                .sorted()
                .forEach(key -> {
                    try {
                        System.out.println(key + "=" + env.getProperty(key));
                    } catch (Exception e) {
                        log.warn("{} -> {}", key, e.getMessage());
                    }
                });
        System.out.println("******************************************************************************");
    }```
Hari
  • 89
  • 1
  • 2
  • 9
  • 1
    SO you want an explanation of your own code? – M. Deinum Sep 30 '19 at 10:44
  • It's not written by me. Code exists in https://stackoverflow.com/questions/24448947/how-to-log-the-active-configuration-in-a-spring-boot-application – Hari Sep 30 '19 at 11:07

0 Answers0