property文件到vo對象的映射工具類


  • 在java開發中經常要用到配置文件,經常需要讀取和保存配置文件,特別是讀取配置文件。但是配置文件默認是String類型的,而配置文件中經常會出現int,long,boolean,double,float等,這就使得我們必須進行類型的轉換,在類型轉換的過程中常常會出錯,所以就開發了這個工具類。
  • 優點
    • 能夠把配置文件映射為一個對象,對象還可定義默認值。
    • 采用反射,打印屬性比較簡潔
    • 減少字符串到具體類型之間的轉換時發生的錯誤

工具類定義

package property;

import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

/**
* VO 對象限制:
* 1. vo必須提供默認的無參數構造函數
* 2. vo必須提供set方法,set方法符合javaBean命名
* 3. vo的成員變量必須和配置文件的key一致
*/

public class Property2Object {

private static Object propertyVO;

//從配置文件的絕對路徑獲取配置
public static Object properties2Object(final String filePath, final Class<?> clazz)
throws FileNotFoundException {
return properties2Object(new FileInputStream(filePath), clazz);
}

//通過文件輸入流獲取配置
public static Object properties2Object(final FileInputStream fileInputStream, final Class<?> clazz) {
Properties properties = new Properties();
InputStream in = new BufferedInputStream(fileInputStream);
try {
properties.load(in);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
return properties2Object(properties, clazz);
}

//通過Property
public static Object properties2Object(final Properties properties, final Class<?> clazz) {

try {
//創建vo對象
propertyVO = clazz.newInstance();
} catch (InstantiationException | IllegalAccessException ex) {
ex.printStackTrace();
}

Method[] methods = clazz.getDeclaredMethods();
for (Method method : methods) {
String name = method.getName();
if (name.startsWith("set")) {
String key = name.substring(3, 4).toLowerCase() + name.substring(4);
String property = properties.getProperty(key);
//未設置屬性采用vo中的默認值,否則采用新值
if (property != null) {
Class<?>[] parameterType = method.getParameterTypes();
String simpleName = parameterType[0].getSimpleName();
Object propertyValue = null;
if (simpleName.equalsIgnoreCase("String")) {
propertyValue = property;
} else if (simpleName.equalsIgnoreCase("int")) {
propertyValue = Integer.parseInt(property);
} else if (simpleName.equalsIgnoreCase("double")) {
propertyValue = Double.parseDouble(property);
} else if (simpleName.equalsIgnoreCase("long")) {
propertyValue = Long.parseLong(property);
} else if (simpleName.equalsIgnoreCase("float")) {
propertyValue = Float.parseFloat(property);
} else if (simpleName.equalsIgnoreCase("boolean")) {
propertyValue = Boolean.parseBoolean(property);
}

try {
//調用set方法設置屬性為配置文件中的值
method.invoke(propertyVO, new Object[]{propertyValue});
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}

}
}
}
return propertyVO;
}

//通過反射打印對象內容,適用於字段很多的情況
public static void printObject(Object object) {

Field[] fields = object.getClass().getDeclaredFields();
for (Field filed : fields) {
try {
filed.setAccessible(true);
System.out.println(filed.getName() + " : " + filed.get(object));
filed.setAccessible(false);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}

}

vo對象

package property;

public class Stu {
//變量名必須與配置文件中相同,可以設置默認值
private String name;
private int age;
private boolean isMan = false;

//默認構造函數必須存在
public Stu() {}

public Stu(String name, int age) {
this.name = name;
this.age = age;
}

//必須有set方法,符合JavaBean規范
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public boolean getIsMan() {
return isMan;
}

public void setIsMan(boolean man) {
isMan = man;
}
}

配置文件內容

name = jieniyimiao
age = 100
isMan = true

測試類

package property;

import java.io.FileNotFoundException;

public class Test {

public static void main(String[] args) throws FileNotFoundException {
Stu stu1 = (Stu) Property2Object.properties2Object(
"C:\\Users\\jieniyimiao\\Desktop\\idea\\src\\stu.property", Stu.class);
Property2Object.printObject(stu1);
}

}

測試結果

name : jieniyimiao
age : 100
isMan : true

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com