Spring 4 - 检索所有属性
Spring 4 - Retrieve all properties
我想要 return Spring 应用程序中使用的所有属性的映射。我在 SO 中发现了几个与此类似的问题,但它们与特定的 属性 文件有关,而我想获取所有属性。
属性应仅适用于当前应用程序 - 不适用于运行时的任何其他部分。
我想出了以下解决方案。
请注意,这里的技巧在于包含或排除的内容。我本可以选择根据 属性 文件路径的某些公共部分来包含,尽管在这种情况下我选择排除 eclipse、gradle 和 jre 属性。
我最初排除项目,但在部署到 Tomcat 时发现我不得不开始排除更多项目。相反,我更改为基于项目名称包含(始终包含公司名称)。代码已更改以反映这一点。
这也有一小部分 Java lambda 代码(双向消费者),但如果需要,可以轻松重写。
public Map<String, Object> getProperties() throws IOException {
if (props != null) {
return props;
}
props = new HashMap<>();
List<String> includeResourcesSubstringList = Arrays.asList(new String[] { "the_company" });
PropertiesFactoryBean propsFactory = new PropertiesFactoryBean();
PathMatchingResourcePatternResolver resResolver = new PathMatchingResourcePatternResolver(
this.getClass().getClassLoader());
Resource[] resources = resResolver.getResources("classpath*:/**/*");
List<Resource> filteredResources = new ArrayList<>();
logger.debug("Exclude resources containing: " + includeResourcesSubstringList);
for (Resource res : resources) {
if (res.getFilename().endsWith(".properties")) {
logger.debug("Res item to inspect: " + res.getDescription());
boolean includeItem = false;
for (String include : includeResourcesSubstringList) {
if (res.getURI() != null && res.getURI().toASCIIString().contains(include)) {
includeItem = true;
break;
}
}
if (includeItem) {
logger.debug("getProperties() - Included resource: " + res.getDescription());
filteredResources.add(res);
}
}
}
propsFactory.setLocations(filteredResources.toArray(new Resource[0]));
propsFactory.afterPropertiesSet();
Properties properties = propsFactory.getObject();
properties.forEach((key, value) -> {
props.put((String) key, value);
});
return props;
}
我想要 return Spring 应用程序中使用的所有属性的映射。我在 SO 中发现了几个与此类似的问题,但它们与特定的 属性 文件有关,而我想获取所有属性。
属性应仅适用于当前应用程序 - 不适用于运行时的任何其他部分。
我想出了以下解决方案。
请注意,这里的技巧在于包含或排除的内容。我本可以选择根据 属性 文件路径的某些公共部分来包含,尽管在这种情况下我选择排除 eclipse、gradle 和 jre 属性。
我最初排除项目,但在部署到 Tomcat 时发现我不得不开始排除更多项目。相反,我更改为基于项目名称包含(始终包含公司名称)。代码已更改以反映这一点。
这也有一小部分 Java lambda 代码(双向消费者),但如果需要,可以轻松重写。
public Map<String, Object> getProperties() throws IOException {
if (props != null) {
return props;
}
props = new HashMap<>();
List<String> includeResourcesSubstringList = Arrays.asList(new String[] { "the_company" });
PropertiesFactoryBean propsFactory = new PropertiesFactoryBean();
PathMatchingResourcePatternResolver resResolver = new PathMatchingResourcePatternResolver(
this.getClass().getClassLoader());
Resource[] resources = resResolver.getResources("classpath*:/**/*");
List<Resource> filteredResources = new ArrayList<>();
logger.debug("Exclude resources containing: " + includeResourcesSubstringList);
for (Resource res : resources) {
if (res.getFilename().endsWith(".properties")) {
logger.debug("Res item to inspect: " + res.getDescription());
boolean includeItem = false;
for (String include : includeResourcesSubstringList) {
if (res.getURI() != null && res.getURI().toASCIIString().contains(include)) {
includeItem = true;
break;
}
}
if (includeItem) {
logger.debug("getProperties() - Included resource: " + res.getDescription());
filteredResources.add(res);
}
}
}
propsFactory.setLocations(filteredResources.toArray(new Resource[0]));
propsFactory.afterPropertiesSet();
Properties properties = propsFactory.getObject();
properties.forEach((key, value) -> {
props.put((String) key, value);
});
return props;
}