java - Spring Boot 读取不带前缀的地图属性
问题描述
我需要在地图中读取 application.properties 文件中的所有属性在下面的代码中,属性测试具有相应的值,但地图为空。如何在不向属性添加前缀的情况下使用 application.properties 文件中的值填充“地图”。
这是我的 application.properties 文件
AAPL=25
GDDY=65
test=22
我正在使用这样的@ConfigurationProperties
@Configuration
@ConfigurationProperties("")
@PropertySource("classpath:application.properties")
public class InitialConfiguration {
private HashMap<String, BigInteger> map = new HashMap<>();
private String test;
public HashMap<String, BigInteger> getMap() {
return map;
}
public void setMap(HashMap<String, BigInteger> map) {
this.map = map;
}
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
}
解决方案
这可以使用PropertiesLoaderUtils和@PostConstruct来实现
请检查以下示例:
@Configuration
public class HelloConfiguration {
private Map<String, String> valueMap = new HashMap<>();
@PostConstruct
public void doInit() throws IOException {
Properties properties = PropertiesLoaderUtils.loadAllProperties("application.properties");
properties.keySet().forEach(key -> {
valueMap.put((String) key, properties.getProperty((String) key));
});
System.err.println("valueMap -> "+valueMap);
}
public Map<String, String> getValueMap() {
return valueMap;
}
public void setValueMap(Map<String, String> valueMap) {
this.valueMap = valueMap;
}
}
推荐阅读
- performance - Haskell:当不需要日志时,让 Writer 和普通代码一样高效
- angular - nrwl/nx 原理图创建 Angular 应用程序并配置 Angular json
- laravel - Laravel 脱离服务器根目录
- java - Java 文件路径 f.exists() 总是返回 false
- sql - OHLC 蜡烛每 X 个刻度
- java - 如何在数组中查找元素
- python - 如何在 Flask 中将列表从一个函数传递到另一个函数?
- python - 使用 spotipy 将曲目添加到播放列表
- ios - Swift:替换 sqlite 文件失败
- c - 将 C 代码中的调试文本打印到 Lauterbach TRACE32