Skip to content

Commit 13da81a

Browse files
authored
Merge pull request dianpoint#6 from dianpoint/initialize-project
Initialize project
2 parents 59d5795 + 3a5a275 commit 13da81a

File tree

6 files changed

+180
-60
lines changed

6 files changed

+180
-60
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.dianpoint.summer.beans;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
/**
9+
* @author: congcong
10+
* @email: congccoder@gmail.com
11+
* @date: 2023/3/17 14:35
12+
*/
13+
public class SimpleBeanFactory implements BeanFactory {
14+
15+
private List<BeanDefinition> beanDefinitions = new ArrayList<>();
16+
17+
private List<String> beanNames = new ArrayList<>();
18+
19+
private Map<String, Object> singletons = new HashMap<>();
20+
21+
public SimpleBeanFactory() {}
22+
23+
@Override
24+
public Object getBean(String beanName) throws NoSuchBeanDefinitionException {
25+
Object singleton = singletons.get(beanName);
26+
if (singleton == null) {
27+
// 此处单例创建未考虑DCL模式,后续优化
28+
int i = beanNames.indexOf(beanName);
29+
if (i == -1) {
30+
throw new NoSuchBeanDefinitionException();
31+
}
32+
BeanDefinition beanDefinition = beanDefinitions.get(i);
33+
try {
34+
singleton = Class.forName(beanDefinition.getClassName()).newInstance();
35+
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
36+
e.printStackTrace();
37+
}
38+
singletons.put(beanDefinition.getId(), singleton);
39+
}
40+
return singleton;
41+
}
42+
43+
@Override
44+
public void registerBeanDefinition(BeanDefinition beanDefinition) {
45+
this.beanDefinitions.add(beanDefinition);
46+
this.beanNames.add(beanDefinition.getId());
47+
}
48+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.dianpoint.summer.beans;
2+
3+
import com.dianpoint.summer.core.Resource;
4+
import org.dom4j.Element;
5+
6+
/**
7+
* <p>
8+
* ClassPathXmlResource类中已经将XML读取为Element元素,通过Reader即可将Resource转化成BeanDefinition
9+
* </p>
10+
*
11+
* @author: congcong
12+
* @email: congccoder@gmail.com
13+
* @date: 2023/3/17 14:25
14+
*/
15+
public class XmlBeanDefinitionReader {
16+
17+
private BeanFactory beanFactory;
18+
19+
public XmlBeanDefinitionReader(BeanFactory beanFactory) {
20+
this.beanFactory = beanFactory;
21+
}
22+
23+
/**
24+
* 从指定的XML文件加载bean定义
25+
*
26+
* @param resource
27+
* XML文件的资源描述
28+
*/
29+
public void loadBeanDefinitions(Resource resource) {
30+
while (resource.hasNext()) {
31+
Element element = (Element)resource.next();
32+
String id = element.attributeValue("id");
33+
String className = element.attributeValue("class");
34+
BeanDefinition beanDefinition = new BeanDefinition(id, className);
35+
this.beanFactory.registerBeanDefinition(beanDefinition);
36+
}
37+
}
38+
}
Lines changed: 30 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,41 @@
11
package com.dianpoint.summer.context;
22

33
import com.dianpoint.summer.beans.BeanDefinition;
4-
import org.dom4j.Document;
5-
import org.dom4j.DocumentException;
6-
import org.dom4j.Element;
7-
import org.dom4j.io.SAXReader;
8-
9-
import java.net.URL;
10-
import java.util.ArrayList;
11-
import java.util.HashMap;
12-
import java.util.List;
13-
import java.util.Map;
4+
import com.dianpoint.summer.beans.BeanFactory;
5+
import com.dianpoint.summer.beans.NoSuchBeanDefinitionException;
6+
import com.dianpoint.summer.beans.SimpleBeanFactory;
7+
import com.dianpoint.summer.beans.XmlBeanDefinitionReader;
8+
import com.dianpoint.summer.core.ClassPathXmlResource;
9+
import com.dianpoint.summer.core.Resource;
1410

1511
/**
1612
* @author: congcong
1713
* @email: congccoder@gmail.com
1814
* @date: 2023/3/17 11:59
1915
*/
20-
public class ClassPathXmlApplicationContext {
21-
22-
private final List<BeanDefinition> beanDefinitions = new ArrayList<>();
23-
24-
private final Map<String, Object> singletons = new HashMap<>();
16+
public class ClassPathXmlApplicationContext implements BeanFactory {
17+
private BeanFactory beanFactory;
2518

2619
/**
27-
* 构造函数读取fileName中外部xml配置,将Bean的定义加载进入内存,并且初始化给BeanDefinition
20+
*
21+
* <ul>
22+
* <li>构造函数读取fileName中外部xml配置,将Bean的定义加载进入内存,并且初始化给BeanDefinition</li>
23+
* <li>context负责整合IOC容器的启动过程,读外部配置,解析Bean定义,创建BeanFactory</li>
24+
* </ul>
2825
*
2926
* @param fileName
3027
* 外部xml配置文件
3128
*/
3229
public ClassPathXmlApplicationContext(String fileName) {
33-
this.readXml(fileName);
34-
this.instanceBeans();
35-
}
36-
37-
/**
38-
* 读取fileName对应xml配置文件。
39-
*
40-
* @param fileName
41-
* 资源文件地址
42-
*/
43-
private void readXml(String fileName) {
44-
SAXReader saxReader = new SAXReader();
45-
try {
46-
URL xmlPath = this.getClass().getClassLoader().getResource(fileName);
47-
Document document = saxReader.read(xmlPath);
48-
Element rootElement = document.getRootElement();
49-
// 遍历xml配置文件,读取每一个`<bean></bean>`配置
50-
for (Element element : rootElement.elements()) {
51-
String id = element.attributeValue("id");
52-
String className = element.attributeValue("class");
53-
// 初始化创建BeanDefinition
54-
BeanDefinition beanDefinition = new BeanDefinition(id, className);
55-
// 存入BeanDefinitions中
56-
beanDefinitions.add(beanDefinition);
57-
}
58-
} catch (DocumentException e) {
59-
e.printStackTrace();
60-
}
61-
}
62-
63-
/**
64-
* 利用反射创建BeanDefinition,并将其存储singletons中
65-
*/
66-
private void instanceBeans() {
67-
for (BeanDefinition beanDefinition : beanDefinitions) {
68-
try {
69-
singletons.put(beanDefinition.getId(), Class.forName(beanDefinition.getClassName()).newInstance());
70-
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
71-
e.printStackTrace();
72-
}
73-
}
30+
// 加载外部xml文件定义为Resource资源
31+
Resource resource = new ClassPathXmlResource(fileName);
32+
33+
BeanFactory factory = new SimpleBeanFactory();
34+
// 解析BeanDefinition定义
35+
XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(factory);
36+
xmlBeanDefinitionReader.loadBeanDefinitions(resource);
37+
// 创建BeanFactory
38+
this.beanFactory = factory;
7439
}
7540

7641
/**
@@ -80,7 +45,13 @@ private void instanceBeans() {
8045
* beanName
8146
* @return beanName对应的class实例
8247
*/
83-
public Object getBean(String beanName) {
84-
return singletons.get(beanName);
48+
@Override
49+
public Object getBean(String beanName) throws NoSuchBeanDefinitionException {
50+
return this.beanFactory.getBean(beanName);
51+
}
52+
53+
@Override
54+
public void registerBeanDefinition(BeanDefinition beanDefinition) {
55+
this.beanFactory.registerBeanDefinition(beanDefinition);
8556
}
8657
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.dianpoint.summer.core;
2+
3+
import org.dom4j.Document;
4+
import org.dom4j.DocumentException;
5+
import org.dom4j.Element;
6+
import org.dom4j.io.SAXReader;
7+
8+
import java.net.URL;
9+
import java.util.Iterator;
10+
11+
/**
12+
* <p>
13+
* ClassPathXmlResource处理xml类型资源
14+
* </p>
15+
*
16+
* @author: congcong
17+
* @email: congccoder@gmail.com
18+
* @date: 2023/3/17 14:19
19+
*/
20+
public class ClassPathXmlResource implements Resource {
21+
22+
private Document document;
23+
private Element rootElement;
24+
private Iterator<Element> elementIterator;
25+
26+
public ClassPathXmlResource(String fileName) {
27+
SAXReader saxReader = new SAXReader();
28+
URL xmlPath = this.getClass().getClassLoader().getResource(fileName);
29+
try {
30+
this.document = saxReader.read(xmlPath);
31+
this.rootElement = document.getRootElement();
32+
this.elementIterator = rootElement.elementIterator();
33+
} catch (DocumentException e) {
34+
e.printStackTrace();
35+
}
36+
}
37+
38+
@Override
39+
public boolean hasNext() {
40+
return this.elementIterator.hasNext();
41+
}
42+
43+
@Override
44+
public Object next() {
45+
return this.elementIterator.next();
46+
}
47+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.dianpoint.summer.core;
2+
3+
import java.util.Iterator;
4+
5+
/**
6+
* <p>
7+
* Resource定义为资源,将外部所有配置信息均理解为资源,使用Resource进行抽象处理
8+
* </p>
9+
*
10+
* @author: congcong
11+
* @email: congccoder@gmail.com
12+
* @date: 2023/3/17 14:20
13+
*/
14+
public interface Resource extends Iterator<Object> {}

src/test/java/com/dianpoint/summer/test/SimpleIOCTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.dianpoint.summer.test;
22

3+
import com.dianpoint.summer.beans.NoSuchBeanDefinitionException;
34
import com.dianpoint.summer.context.ClassPathXmlApplicationContext;
45
import com.dianpoint.summer.test.service.SimpleService;
56

@@ -10,7 +11,8 @@
1011
*/
1112
public class SimpleIOCTest {
1213

13-
public static void main(String[] args) {
14+
15+
public static void main(String[] args) throws NoSuchBeanDefinitionException {
1416
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
1517
SimpleService simpleService = (SimpleService)applicationContext.getBean("simpleService");
1618
simpleService.sayHello();

0 commit comments

Comments
 (0)