简体   繁体   English

如何在Spring Boot 1应用程序中查找未使用的属性

[英]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... Spring Boot 1应用程序中是否有办法了解Spring Boot应用程序使用的有效属性与使用YAML设置的所有属性,系统属性...

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. 理想情况下,如果我们在框架中维护任何过时的属性,我想在ApplicationListener类中进行检测,并阻止应用程序启动。

Thx by advance for your help, 提前感谢您的帮助,

Eric 埃里克

What I did when I had the same requirement was creating my own PropertyPlaceholderConfigurer: 当我有相同的要求时,我所做的就是创建自己的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. 然后,您可以注册一些CommandLineListener或ApplicationEvent侦听器,以在应用程序启动时调用getPropertyNames()。

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. 您可以对两个列表进行排序并进行比较以过滤掉未使用的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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