标签:eth uppercase 根据 version 底层实现 new element declared book
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Book {
private Integer id;
private String name;
private Double price;
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="threeBody" class="per.tan.ioc.Book">
<property name="id" value="1"/>
<property name="name" value="《三体》"/>
<property name="price" value="59.9"/>
</bean>
</beans>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.1</version>
</dependency>
private final Map<String, Object> iocMap;
public MyClassPathXmlApplicationContext(String path) {
iocMap = new HashMap<>();
parseXML(path);
}
//使用Dom4j进行xml解析
public void parseXML(String path) {
SAXReader saxReader = new SAXReader();
try {
//定义文档
Document document = saxReader.read("src/main/resources/" + path);
//获取根节点,即<beans>
Element beans = document.getRootElement();
//beans迭代器
Iterator<Element> beansIterator = beans.elementIterator();
while (beansIterator.hasNext()) {
Element bean = beansIterator.next();
String idStr = bean.attributeValue("id");
String classStr = bean.attributeValue("class");
Class<?> clazz = Class.forName(classStr);
Constructor<?> constructor = clazz.getConstructor();
//利用获取到的无参构造生成目标对象
Object object = constructor.newInstance();
//bean迭代器
Iterator<Element> beanIterator = bean.elementIterator();
while (beanIterator.hasNext()) {
Element property = beanIterator.next();
//获取属性
String propertyName = property.attributeValue("name");
String propertyValue = property.attributeValue("value");
Field field = clazz.getDeclaredField(propertyName);
//获取方法
String methodStr = "set" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
Method method = clazz.getMethod(methodStr, field.getType());
Object value = propertyValue;
//判断属性类型并转换
switch (field.getType().getName()) {
case "java.lang.Integer":
value = Integer.parseInt(propertyValue);
break;
case "java.lang.Double":
value = Double.parseDouble(propertyValue);
break;
}
method.invoke(object, value);
}
//装入容器
iocMap.put(idStr, object);
}
} catch (DocumentException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
@Override
public Object getBean(String s) throws BeansException {
return iocMap.get(s);
}
public class Test {
public static void main(String[] args) {
ApplicationContext context = new MyClassPathXmlApplicationContext("spring.xml");
Book book = (Book) context.getBean("threeBody");
System.out.println(book);
}
}
D:\Java_JDK\JDK8\bin\java.exe ...
Book(id=1, name=《三体》, price=59.9)
Process finished with exit code 0
标签:eth uppercase 根据 version 底层实现 new element declared book
原文地址:https://www.cnblogs.com/200ok/p/14773185.html