java - 将自定义属性文件转换为java中的复杂类型对象
问题描述
我在 java 1.8 中构建了一个自定义属性文件,我可以读取它的属性,但我想做的是我可以得到这个带有复杂类型对象的属性文件。我不想用键和值读取我想直接将它转换为对象,我怎样才能做到这一点
//Connection properties is my custom properties file class
prop = new Properties();
String filename = "connection.properties";
input = ConnectionProperties.class.getClassLoader().getResourceAsStream(filename);
if(input==null){
System.out.println("Sorry, unable to find " + filename);
return;
}
//load a properties file from class path, inside static method
prop.load(input);
Enumeration<?> e = prop.propertyNames();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
String value = prop.getProperty(key);
System.out.println("Key : " + key + ", Value : " + value);
}
解决方案
您可以@ConfigurationProperties
从 SpringBoot 使用。
在此处阅读更多信息:https ://www.baeldung.com/configuration-properties-in-spring-boot
推荐阅读
- javascript - React 组件 type.name 是一些抽象字母
- spring - 使用Spring时如何在一个方面访问Jersey的上下文对象?
- xslt - Camel xslt 组件从给定的 xml 数据创建静态 sql 查询
- python - Wagtail ImageChooserPanel 给出错误`没有这样的列:projects_projectblogdetail.project_image_id`
- r - R中的TreeMap对hmax的某些值给出错误
- java - 使用 Quarkus 在 MongoDb 中注册编解码器
- c# - 如何刷新环境变量进程快照?
- python - 如何将具有特定位置的值的新键插入字典?
- html - 在 Woocommerce 中动态加载 trustpilot 评论
- antd - 如何根据值更改 ant design 进度条颜色?