解析properties,并封装到指定对象
封装类 User.java
package Reflect_class.ProAndXml;
public class User {
private String userName;
private String passWord;
private String age;
public User() {
}
public User(String userName, String passWord, String age) {
this.userName = userName;
this.passWord = passWord;
this.age = age;
}
public User(String userName){
this.userName = userName;
}
@Override
public String toString() {
return "User{" +
"userName='" + userName + '\'' +
", passWord='" + passWord + '\'' +
", age='" + age + '\'' +
'}';
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
配置文件 user.properties
user.class =Reflect_class.ProAndXml.User
user.userName = jack
user.passWord = 1234
user.age = 18
测试类 propertiesTest.java
package Reflect_class.ProAndXml;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Properties;
public class propertiesTest2 {
public static void main(String[] args) throws Exception {
String file = "D:\\Java文件\\A高级Java代码\\Reflect\\src\\Reflect_class\\ProAndXml\\user.properties";
//1.读取properties
//工具类
Properties props = new Properties();
//2.加载文件
props.load(new FileInputStream(file));
//3.获得数据
String userClass = props.getProperty("user.class");
String userName = props.getProperty("user.userName");
String passWord = props.getProperty("user.passWord");
String age = props.getProperty("user.age");
//获得Class
Class clazz = Class.forName(userClass);
//获得实例,封装userName
Constructor cons = clazz.getConstructor(String.class);
Object obj = cons.newInstance(userName);
//封装实例(字段passWord,方法age)
Field passWordField = clazz.getDeclaredField("passWord");
passWordField.setAccessible(true);
passWordField.set(obj,passWord);
//方法age user.setAge(age)
Method ageMethod = clazz.getMethod("setAge",String.class);
ageMethod.invoke(obj,age);
System.out.println(obj);
}
}
运行结果
![](https://i-blog.csdnimg.cn/blog_migrate/e0b421e783fea73a203f8c0fdc230c71.png)
解析xml,并封装到指定对象
配置文件
![](https://i-blog.csdnimg.cn/blog_migrate/2be5365e30b4235a6c87c59853c98039.png)
封装类Student.class
package Reflect_class.ProAndXml;
public class Student2 {
private String studentName;
private String gender;
private Integer age;
public Student2() {
}
public Student2(String studentName, String gender, Integer age) {
this.studentName = studentName;
this.gender = gender;
this.age = age;
}
@Override
public String toString() {
return "Student2{" +
"studentName='" + studentName + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
'}';
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
配置文件 Student.xml
<?xml version="1.0" encoding="UTF-8" ?>
<beans>
<bean id="student1" class="Reflect_class.ProAndXml.Student2">
<property name="studentName" value="张三"/>
<property name="gender" value="男"/>
<property name="age" value="18"/>
</bean>
<bean id="student2" class="Reflect_class.ProAndXml.Student2">
<property name="studentName" value="李四"/>
<property name="gender" value="女"/>
<property name="age" value="21"/>
</bean>
<bean id="user1" class="Reflect_class.ProAndXml.User">
<property name="userName" value="jack"/>
<property name="passWord" value="1234"/>
<property name="age" value="18"/>
</bean>
</beans>
测试类
步骤:
![](https://i-blog.csdnimg.cn/blog_migrate/78616cb4262a32dac39a151c59e478c2.png)
1.解析xml
package Reflect_class.ProAndXml;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.File;
import java.util.List;
public class xmlTest2 {
public static void main(String[] args) throws Exception{
File file = new File("D:\\Java文件\\A高级Java代码\\Reflect\\src\\Reflect_class\\ProAndXml\\Student.xml");
//1.xml工具类
SAXReader saxReader = new SAXReader();
//2.加载xml.获得根元素document
Document document = saxReader.read(file);
//3.获得根元素
Element root = document.getRootElement();
//4.获得所有bean
List<Element> allBean = root.elements("bean");
//###创建Map存放所有数据 Map<String,Object>
//5.遍历
for (Element beanElement : allBean) {
//6.获得id属性
String beanId = beanElement.attributeValue("id");
//7.获得class属性
String beanClass = beanElement.attributeValue("class");
//###获得Class
//###获得实例
//###添加Map中
//8.获得所有子元素property
List<Element> property = beanElement.elements("property");
//9.遍历properties
for (Element propertyElement : property) {
//10.获得name属性
String propertyName = propertyElement.attributeValue("name");
//11.获得value属性
String propertyValue = propertyElement.attributeValue("value");
}
//###数据封装到实例中,字段(私有)
}
//遍历Map,打印
}
}
2.完整代码 xmlTest.class
package Reflect_class.ProAndXml;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.File;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class xmlTest2 {
public static void main(String[] args) throws Exception{
File file = new File("D:\\Java文件\\A高级Java代码\\Reflect\\src\\Reflect_class\\ProAndXml\\StudentXml2.xml");
//1.xml工具类
SAXReader saxReader = new SAXReader();
//2.加载xml.获得根元素document
Document document = saxReader.read(file);
//3.获得根元素
Element root = document.getRootElement();
//4.获得所有bean
List<Element> allBean = root.elements("bean");
//###创建Map存放所有数据 Map<String,Object>
Map<String,Object> studentMap = new HashMap<>();
//5.遍历
for (Element beanElement : allBean) {
//6.获得id属性
String beanId = beanElement.attributeValue("id");
//7.获得class属性
String beanClass = beanElement.attributeValue("class");
//###获得Class
Class clazz = Class.forName(beanClass);
//###获得实例
Object obj = clazz.newInstance();
//###添加Map中
studentMap.put(beanId,obj);
//8.获得所有子元素property
List<Element> property = beanElement.elements("property");
//9.遍历properties
for (Element propertyElement : property) {
//10.获得name属性
String propertyName = propertyElement.attributeValue("name");
//11.获得value属性
String propertyValue = propertyElement.attributeValue("value");
//###数据封装到实例中,字段(私有)
//##获得属性Field
Field field = clazz.getDeclaredField(propertyName);
//##设置权限
field.setAccessible(true);
//获得类型
Class propTypeClass = field.getType();
//##赋值(1.支持字符串)
if (propTypeClass == String.class){
field.set(obj,propertyValue);
}
//##(2.支持整型)
if (propTypeClass == Integer.class){
field.set(obj,Integer.parseInt(propertyValue));
}
}
}
//遍历Map,打印
for (Map.Entry<String, Object> stringObjectEntry : studentMap.entrySet()) {
System.out.print(stringObjectEntry.getKey());
System.out.print(":");
System.out.println(stringObjectEntry.getValue());
}
}
}
运行结果
![](https://i-blog.csdnimg.cn/blog_migrate/60311de515da7cb878d869eed2609925.png)