12

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

Darth Ninja
  • 1,049
  • 2
  • 11
  • 34
  • 1
    Possible duplicate of [Spring: access all Environment properties as a Map or Properties object](https://stackoverflow.com/questions/23506471/spring-access-all-environment-properties-as-a-map-or-properties-object) – Cherry May 29 '17 at 02:24

3 Answers3

5

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();
        }
    }
}
fl4l
  • 1,580
  • 4
  • 21
  • 27
4

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.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
3

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);
    }
}
amgohan
  • 1,358
  • 3
  • 14
  • 24
  • Weakness of this solution is that it prints all sources property mapping individually possibly duplicated values for overrided keys instead of printing only the result values for all properties . – Daniel Hári Mar 01 '17 at 20:39