Printing all properties set via Spring PropertyPlaceholderConfigurer

后端 未结 3 2069
执念已碎
执念已碎 2021-02-14 09:08

I’d like to print the consolidated list of properties set in our application on startup. What is the best way to do this?

Thanks

相关标签:
3条回答
  • 2021-02-14 09:36

    Here is a concrete example of printing all properties :

    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
    import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
    import org.springframework.web.context.support.StandardServletEnvironment;
    
    public class PropertiesLoaderConfigurer
        extends PropertySourcesPlaceholderConfigurer {
    
        private static final String ENVIRONMENT_PROPERTIES = "environmentProperties";
    
        @Override
        public void postProcessBeanFactory(
            final ConfigurableListableBeanFactory beanFactory)
            throws BeansException {
    
            super.postProcessBeanFactory(beanFactory);
    
            final StandardServletEnvironment propertySources =
                (StandardServletEnvironment) super.getAppliedPropertySources().get(ENVIRONMENT_PROPERTIES).getSource();
    
            propertySources.getPropertySources().forEach(propertySource -> {
                if (propertySource.getSource() instanceof Map) {
                    // it will print systemProperties, systemEnvironment, application.properties and other overrides of
                    // application.properties
                    System.out.println("#######" + propertySource.getName() + "#######");
    
                    final Map<String, String> properties = mapValueAsString((Map<String, Object>) propertySource.getSource());
                    System.out.println(properties);
                }
            });
        }
    
        private Map<String, String> mapValueAsString(
            final Map<String, Object> map) {
    
            return map.entrySet().stream()
                .collect(Collectors.toMap(entry -> entry.getKey(), entry -> toString(entry.getValue())));
        }
    
        private String toString(
            final Object object) {
    
            return Optional.ofNullable(object).map(value -> value.toString()).orElse(null);
        }
    }
    
    0 讨论(0)
  • 2021-02-14 09:37

    This is my implementation:

    public class CustomPropertySourcesPlaceholderConfigurer extends PropertySourcesPlaceholderConfigurer implements InitializingBean{
    
        public void afterPropertiesSet(){
            try{
                Properties loadedProperties = this.mergeProperties();
                for(Entry<Object, Object> singleProperty : loadedProperties.entrySet()){
                    logger.info("LoadedProperty: "+singleProperty.getKey()+"="+singleProperty.getValue());
                }
            }catch(Exception ex){
                ex.printStackTrace();
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-14 09:38

    Use a custom PropertyPlaceholderConfigurer implementation that overrides the resolve... methods and logs the placeholder name. You may also need/want to override the convert... methods, but resolve... should handle it.

    0 讨论(0)
提交回复
热议问题