结构
现有properties文件两个:bean.properties和data.properties
bean.properties:
1 id=id001 2 className=com.domain.User
data.properties:
1 uid=u001 2 userName=jack 3 password=1234
两个类:beanConfig.java和User.java
beanConfig.java:
1 package com.domain; 2 3 import java.util.Properties; 4 5 public class BeanConfig { 6 private String id; 7 private String className; 8 Properties properties = new Properties(); 9 10 public BeanConfig(String id, String className, Properties properties) { 11 this.id = id; 12 this.className = className; 13 this.properties = properties; 14 } 15 16 public BeanConfig() { 17 } 18 19 public String getId() { 20 return id; 21 } 22 23 public void setId(String id) { 24 this.id = id; 25 } 26 27 public String getClassName() { 28 return className; 29 } 30 31 public void setClassName(String className) { 32 this.className = className; 33 } 34 35 public Properties getProperties() { 36 return properties; 37 } 38 39 public void setProperties(Properties properties) { 40 this.properties = properties; 41 } 42 43 @Override 44 public String toString() { 45 return "BeanConfig{" + 46 "id='" + id + '\'' + 47 ", className='" + className + '\'' + 48 ", properties=" + properties + 49 '}'; 50 } 51 }
User.java:
1 package com.domain; 2 3 public class User { 4 private String uid; 5 private String userName; 6 private String password; 7 8 public User(String uid, String userName, String password) { 9 this.uid = uid; 10 this.userName = userName; 11 this.password = password; 12 } 13 14 public User() { 15 } 16 17 public String getUid() { 18 return uid; 19 } 20 21 public void setUid(String uid) { 22 this.uid = uid; 23 } 24 25 public String getUserName() { 26 return userName; 27 } 28 29 public void setUserName(String userName) { 30 this.userName = userName; 31 } 32 33 public String getPassword() { 34 return password; 35 } 36 37 public void setPassword(String password) { 38 this.password = password; 39 } 40 41 @Override 42 public String toString() { 43 return "User{" + 44 "uid='" + uid + '\'' + 45 ", userName='" + userName + '\'' + 46 ", password='" + password + '\'' + 47 '}'; 48 } 49 }
方法
将两个.properties文件信息封装到 beanConfig.java中,接着用反射机制将数据封装到User的实例中
***********************************************************************************************************************************************************************************************************
具体操作
1 package com.test; 2 3 import com.domain.BeanConfig; 4 import org.junit.Test; 5 6 import java.io.FileInputStream; 7 import java.io.InputStreamReader; 8 import java.lang.reflect.Method; 9 import java.util.Properties; 10 11 public class Demo_03 { 12 //获取配置文件信息 13 public BeanConfig getBeanConfig() throws Exception{ 14 BeanConfig beanConfig = new BeanConfig(); 15 //读取bean.properties文件 16 Properties beanProperties = new Properties(); 17 beanProperties.load(new InputStreamReader(new FileInputStream("bean.properties"),"UTF-8")); 18 beanConfig.setId(beanProperties.getProperty("id")); 19 beanConfig.setClassName(beanProperties.getProperty("className")); 20 21 //读取data.properties文件 22 Properties dataProperties = new Properties(); 23 dataProperties.load(new InputStreamReader(new FileInputStream("data.properties"),"UTF-8")); 24 for(String name:dataProperties.stringPropertyNames()){ 25 String dataVlaue = dataProperties.getProperty(name); 26 beanConfig.getProperties().setProperty(name,dataVlaue); 27 } 28 return beanConfig; 29 } 30 31 @Test 32 public void demo_01() throws Exception{ 33 BeanConfig beanConfig = getBeanConfig(); 34 Class clazz = Class.forName(beanConfig.getClassName()); 35 Object object = clazz.newInstance(); 36 for(String name:beanConfig.getProperties().stringPropertyNames()){ 37 //***遍历方式*** 38 String value = beanConfig.getProperties().getProperty(name); 39 String methodName = "set"+name.substring(0,1).toUpperCase()+name.substring(1); 40 Method method = clazz.getMethod(methodName,String.class); 41 method.invoke(object,value); 42 } 43 System.out.println(object); 44 } 45 }
运行结果
^_^