这里我用 maven项目 作为 演示
配置pom.xml文件
完整的pom.xml文件信息
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.oukele.Readerxml</groupId>
<artifactId>ReaderXml</artifactId>
<version>1.0-SNAPSHOT</version> <name>ReaderXml</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<!--添加依赖-->
<dependencies> <!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency> <!-- dom4j 依赖-->
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.1</version>
</dependency> </dependencies> <build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build> </project>
项目结构
Cat类(Dog类 类似)截图:
接口IBeans
在main文件夹下面新建一个resources文件夹
在resources文件夹中新建一个xml(文件名字自定义)文件
比如:
test_xml.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!--自定义 父节点-->
<beans> <!--自定义 子节点-->
<beans id="cat" class="com.oukele.Readerxml.animal.Cat" ></beans> <beans id="dog" class="com.oukele.Readerxml.animal.Dog" ></beans> </beans>
Beans类代码:
package com.oukele.Readerxml.ioc; import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader; import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map; public class Beans implements IBeans{ private static Map<String,Object> banes = new HashMap<>(); public Beans(String configxml) throws Exception {
//创建SAXReader对象
SAXReader reader = new SAXReader();
//xml文件的位置
URL sources = Beans.class.getClassLoader().getResource(configxml);
//创建document对象,并读取xml文件 (解析xml文件)
Document document = reader.read(sources);
//读取元素 getRootElement() --》 获取父节点 elements() --> 所有节点
List<Element> elements = document.getRootElement().elements(); for (Element element : elements) {
// <beans id="cat" class="com.oukele.Readerxml.animal.Cat" ></beans>
// id 是一个属性 可以自定义
String id = element.attributeValue("id");//获取id属性
String clazz = element.attributeValue("class");//获取class属性
// id (钥匙) 对应 一个 实例
banes.put(id,Class.forName(clazz).newInstance());
} } @Override
public Object getBean(String name) {
//根据名字来获取实例
return banes.get(name);
}
}
去test文件中的AppTest类中测试一下
package com.oukele.Readerxml; import com.oukele.Readerxml.animal.Cat;
import com.oukele.Readerxml.animal.Dog;
import com.oukele.Readerxml.ioc.Beans;
import org.junit.Test; public class AppTest
{
@Test
public void Test1(){
try { Beans beans = new Beans("test_xml.xml"); Cat cat = (Cat) beans.getBean("cat");
cat.say(); System.out.println("----------------------------------"); Dog dog = (Dog) beans.getBean("dog");
dog.say(); } catch (Exception e) {
e.printStackTrace();
}
}
}
结果: