根据属性文件中的匹配条目调用bean样式的getter

时间:2014-08-22 04:58:02

标签: java

我正在寻找一种自动方式来读取属性文件并在Java对象上调用相应的setter。

#property file
person.firstName=firstName
person.age=11
person.address.cityName=cityName
...

请使用以下示例对象模型。

public class Person{
   // below have getters & setters
   private String firstName;
   private int age;
   private Address = new Address(); // not null
}

public class Address{
   //below have setters and getters
   private cityName;
   ...
}

我正在寻找什么

Person personObj = new Person();
MagicTransformer mt = new MagicTransformer();
mt.doMagic("filePath", personObj);
//now the personObj has all the properties set from the file.

如果框架对转换标准类型很聪明,那就太棒了:基元,字符串,枚举,列表(基元和字符串),地图(基元和字符串)

我在发帖前探讨过。任何与阅读属性文件相关的搜索'已经归还了Spring的房产占位符等。我可能错过了资源。如果你能说出几个框架,那就太棒了。

2 个答案:

答案 0 :(得分:0)

不知道有一个lib可以做到这一点,但它应该是相当微不足道的:

答案 1 :(得分:0)

如何将数据存储在xm中;并使用xmlbeans

一个简单的例子是

File xmlFile = new File("c:\employees.xml"); 

// Bind the instance to the generated XMLBeans types.

EmployeesDocument empDoc = 
    EmployeesDocument.Factory.parse(xmlFile); 

// Get and print pieces of the XML instance.
Employees emps = empDoc.getEmployees(); 
Employee[] empArray = emps.getEmployeeArray(); 
for (int i = 0; i < empArray.length; i++) 
{ 
    System.out.println(empArray[i]); 
}