简体   繁体   中英

How to find unused properties in a spring boot 1 application

Is there a way in a spring boot 1 application to know the effective properties used by a Spring Boot application versus all properties setted up with YAML, system properties...

Ideally I would like to detect that in an ApplicationListener class and prevent the application to start if there are any obsolete properties that we maintain in our framework.

Thx by advance for your help,

Eric

What I did when I had the same requirement was creating my own PropertyPlaceholderConfigurer:

public class DisplayablePropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {

    private static final Logger log = LoggerFactory.getLogger(DisplayablePropertyPlaceholderConfigurer.class);

    private int processedNum;

    private HashSet<String> propertyNames = new HashSet<>();

    @Override
    protected void doProcessProperties(ConfigurableListableBeanFactory beanFactoryToProcess, StringValueResolver valueResolver) {
        super.doProcessProperties(beanFactoryToProcess, valueResolver);
    }

    @Override
    protected String resolvePlaceholder(String placeholder, Properties props, int systemPropertiesMode) {
        return super.resolvePlaceholder(placeholder, props, systemPropertiesMode);
    }

@Override
protected String resolvePlaceholder(String placeholder, Properties props) {
    propertyNames.add(placeholder);
    return super.resolvePlaceholder(placeholder, props);
}

    @Override
    protected String resolveSystemProperty(String key) {
        return super.resolveSystemProperty(key);
    }

    public HashSet<String> getPropertyNames() {
        return propertyNames;
    }
}

You could then register some CommandLineListener or ApplicationEvent listener to call getPropertyNames() on application startup.

After that you will get a list of used properties. It's a good point to start, isn't it? You can sort both lists and compare to filter out properties that are not used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM