java - 如何在 Spring Boot 1 应用程序中查找未使用的属性
问题描述
在 Spring Boot 1 应用程序中是否有办法知道 Spring Boot 应用程序使用的有效属性与使用 YAML 设置的所有属性、系统属性...
理想情况下,如果我们在框架中维护任何过时的属性,我想在 ApplicationListener 类中检测到这一点并阻止应用程序启动。
感谢您的帮助,
埃里克
解决方案
当我有同样的需求时,我所做的是创建自己的 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;
}
}
然后,您可以注册一些 CommandLineListener 或 ApplicationEvent 侦听器以在应用程序启动时调用 getPropertyNames()。
之后,您将获得已使用属性的列表。这是一个很好的起点,不是吗?您可以对这两个列表进行排序并进行比较以过滤掉未使用的属性。
推荐阅读
- algorithm - 我在 Maple 中的多项式 GCD 实现给出了错误的结果
- javascript - React js tsx codesandbox问题 抛出了跨域错误,lodash.isequal
- react-native - 编译android时React-native-camera错误,react-native 0.60.5
- javascript - 如何返回带有角度可观察的数字/变量?
- ansible - 如何在一台服务器上使用分子 prepare.yml 而在另一台服务器上运行 playbook?
- css - css问题文本一直在列之外移动,并且标题在滚动时不会保留在原位
- c# - 修改后无法另存为 .docx 文件
- sql - 在sql oracle中添加排序列
- c++ - pjsip TLS connect() 错误:[code=70006]:在 Mac 上未找到 (PJ_ENOTFOUND),但未指定原因
- asp.net - 如何使用 C# 在 ASP.NET 中使用 Crystal Reports 打印 datagidview?