要打印在Spring Boot应用程序启动期间加载的属性

时间:2019-09-30 10:38:55

标签: spring-boot spring-boot-maven-plugin spring-boot-admin

任何人都可以解释以下代码中发生的事情吗?

  1. MapPropertySource的用途是什么?
  2. 除了MapPropertySource源和applicationConfig之外,还有哪些其他选项?
  3. 我们为什么要过滤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("******************************************************************************");
    }```

0 个答案:

没有答案