1.beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
">
<!--
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/affairs" />
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
-->
<bean id="student" class="testspring.bean.Student" >
<!-- collaborators and configuration for this bean go here -->
<property name = "clazz" value = "三年二班"></property>
</bean>
</beans>
2.pojo类Student
/**
*
*/
package testspring.bean;
/**
* @author 无痕菌
*
*/
public class Student {
private int id = 1;
private String name = "张三";
private String clazz = "大四一班";
/**
* @return id
*/
public int getId() {
return id;
}
/**
* @param id 要设置的 id
*/
public void setId(int id) {
this.id = id;
}
/**
* @return name
*/
public String getName() {
return name;
}
/**
* @param name 要设置的 name
*/
public void setName(String name) {
this.name = name;
}
/**
* @return clazz
*/
public String getClazz() {
return clazz;
}
/**
* @param clazz 要设置的 clazz
*/
public void setClazz(String clazz) {
this.clazz = clazz;
} }
3.dom4j实现spring的getbean功能的模拟
/**
*
*/
package springReadBeanTest;
import java.io.File;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import testspring.bean.Student;
/**
* @author 无痕菌
*
*/
public class Dom4jXmlParseGetBean {
/**
*作者:alice
* 日期:2022年1月21日
*返回值:void
*参数: @param args
* @throws DocumentException
*/
public static void main(String[] args) throws DocumentException {
File file = new File("src/springReadBeanTest/Beans.xml");
SAXReader reader = new SAXReader();
// 读取文件作为文档
Document doc = reader.read(file);
// 获取文档的根元素
Element root = doc.getRootElement();
// 根据跟元素找到全部的子节点
Iterator<Element> iter = root.elementIterator();
while(iter.hasNext()){
Element name = iter.next();
if(name.getName().contentEquals("bean")){
String aString = name.attributeValue("class");
System.err.println(aString);
try {
Student student = (Student) Class.forName(aString).newInstance();
System.err.println(student.getName());
} catch (InstantiationException|IllegalAccessException |ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
//System.out.println("value = " + name.getText());
}
/* 使用DocumentHelper来创建 Document对象
Document document = DocumentHelper.createDocument();
// 创建元素并设置关系
Element person = document.addElement("person");
Element name = person.addElement("name");
Element age = person.addElement("age");
// 设置文本
name.setText("shsxt");
age.setText("10");
// 创建格式化输出器
OutputFormat of = OutputFormat.createPrettyPrint();
of.setEncoding("utf-8");
// 输出到文件
File file = new File("resource/outputdom4j.xml");
XMLWriter writer = new XMLWriter(new FileOutputStream(new
File(file.getAbsolutePath())),of);
// 写出
writer.write(document);
writer.flush();
writer.close();
*/
}
}
}