-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathApplicationContext.java
More file actions
55 lines (49 loc) · 1.69 KB
/
Copy pathApplicationContext.java
File metadata and controls
55 lines (49 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package reflect_spring;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class ApplicationContext {
//缓存Spring容器中的Bean对象
private Map<String, Object> beans = new HashMap<>();
/*
* 利用配置文件初始化当前容器
* 利用xml配置文件,初始化全部的Bean对象
*/
public ApplicationContext(String xml) throws Exception {
//利用DOM4j读取xml文件
//解析XML文件内容,得到Bean的类名和Bean的id
//根据类名动态加载类并且创建对象
//将对象和对应的id添加到Map缓存中
//从Resourse(classpath)中读取流
InputStream in = getClass().getClassLoader().getResourceAsStream(xml);
SAXReader reader = new SAXReader(); //相当于一个高级流,高级流需依附于低级流
Document doc = reader.read(in);
in.close();
//解析xml:<beans><bean><bean>...<beans>
Element root = doc.getRootElement(); //Element就是beans
//读取根元素中全部的bean子元素.
List<Element> list = root.elements("bean");
for (Element e : list) {
//e就是bean元素 属性: id, class
String id = e.attributeValue("id"); //获取属性
String className = e.attributeValue("class");
//动态加载类,动态创建对象
Class clazz = Class.forName(className);
Object bean = clazz.newInstance();
beans.put(id, bean);
}
}
public Object getBean(String id) {
//根据id在Map中查找对象,并返回对象
return beans.get(id);
}
//泛型方法
public <T> T getBean(String id, Class<T> cls) {
return (T) beans.get(id);
}
}