Skip to content

Commit 1b091c5

Browse files
committed
docs
1 parent 428e4c7 commit 1b091c5

File tree

6 files changed

+95
-11
lines changed

6 files changed

+95
-11
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
11
tiny-spring
22
=======
33

4-
A tiny IoC container refer to Spring.
4+
>A tiny IoC container refer to Spring.
5+
6+
## 关于
7+
8+
`tiny-spring`是为了学习Spring的而开发的,可以认为是一个Spring的精简版。Spring的代码很多,层次复杂,阅读起来费劲。我尝试从使用功能的角度出发,参考Spring的实现,一步一步构建,最终完成一个精简版的Spring。
9+
10+
## 功能
11+
12+
1. 支持singleton类型的bean,包括初始化、属性注入、以及依赖bean注入。
13+
2. 可从xml中读取配置。
14+
3. 可以使用Aspectj的方式进行AOP编写,支持接口和类代理。
15+
16+
## 使用
17+
18+
`tiny-spring`是逐步进行构建的,里程碑版本我都使用了git tag来管理。例如,最开始的tag是`step-1-container-register-and-get`,那么可以使用
19+
20+
git checkout step-1-container-register-and-get
21+
22+
来获得这一版本。版本历史见`changelog.md`

changelog.md

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,61 @@
11
tiny-spring
22
=====
33

4-
步骤:
4+
# 第一部分:IoC容器
55

66
## 1.step1-最基本的容器
7-
*step-1-container-register-and-get*
7+
8+
git checkout step-1-container-register-and-get
9+
10+
IoC最基本的角色有两个:容器(`BeanFactory`)和Bean本身。这里使用`BeanDefinition`来封装了bean对象,这样可以保存一些额外的元信息。测试代码:
11+
12+
```java
13+
// 1.初始化beanfactory
14+
BeanFactory beanFactory = new BeanFactory();
15+
16+
// 2.注入bean
17+
BeanDefinition beanDefinition = new BeanDefinition(new HelloWorldService());
18+
beanFactory.registerBeanDefinition("helloWorldService", beanDefinition);
819

9-
单纯的map,有get和put bean的功能
20+
// 3.获取bean
21+
HelloWorldService helloWorldService = (HelloWorldService) beanFactory.getBean("helloWorldService");
22+
helloWorldService.helloWorld();
23+
```
1024

1125
## 2.step2-将bean创建放入工厂
12-
*step-2-abstract-beanfactory-and-do-bean-initilizing-in-it*
1326

14-
1. 抽象beanfactory
15-
2. 将bean初始化放入beanfactory
27+
git checkout step-2-abstract-beanfactory-and-do-bean-initilizing-in-it
28+
29+
step1中的bean是初始化好之后再set进去的,实际使用中,我们希望容器来管理bean的创建。于是我们将bean的初始化放入BeanFactory中。为了保证扩展性,我们使用Extract Interface的方法,将`BeanFactory`替换成接口,而使用`AbstractBeanFactory``AutowireCapableBeanFactory`作为其实现。"AutowireCapable"的意思是“可自动装配的”,为我们后面注入属性做准备。
30+
31+
```java
32+
// 1.初始化beanfactory
33+
BeanFactory beanFactory = new AutowireCapableBeanFactory();
34+
35+
// 2.注入bean
36+
BeanDefinition beanDefinition = new BeanDefinition();
37+
beanDefinition.setBeanClassName("us.codecraft.tinyioc.HelloWorldService");
38+
beanFactory.registerBeanDefinition("helloWorldService", beanDefinition);
39+
40+
// 3.获取bean
41+
HelloWorldService helloWorldService = (HelloWorldService) beanFactory.getBean("helloWorldService");
42+
helloWorldService.helloWorld();
43+
```
1644

1745
## 3.step3-为bean注入属性
18-
*step-3-inject-bean-with-property*
46+
47+
git checkout step-3-inject-bean-with-property
1948

2049
## 4.step4-读取xml配置来初始化bean
21-
*step-4-config-beanfactory-with-xml*
50+
51+
git checkout step-4-config-beanfactory-with-xml
2252

2353
## 5.step5-为bean注入bean
24-
*step-5-inject-bean-to-bean*
54+
55+
git checkout step-5-inject-bean-to-bean
2556

2657
## 6.step6-ApplicationContext登场
27-
*step-6-invite-application-context*
58+
59+
git checkout step-6-invite-application-context
60+
61+
# AOP

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
<version>4.7</version>
3636
<scope>test</scope>
3737
</dependency>
38+
<dependency>
39+
<groupId>aopalliance</groupId>
40+
<artifactId>aopalliance</artifactId>
41+
<version>1.0</version>
42+
</dependency>
3843
</dependencies>
3944

4045
<build>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package us.codecraft.tinyioc.aop;
2+
3+
/**
4+
* @author yihua.huang@dianping.com
5+
*/
6+
public class AdvisedSupport {
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package us.codecraft.tinyioc.aop;
2+
3+
/**
4+
* @author yihua.huang@dianping.com
5+
*/
6+
public interface AopProxy {
7+
8+
Object getProxy();
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package us.codecraft.tinyioc.aop;
2+
3+
/**
4+
* @author yihua.huang@dianping.com
5+
*/
6+
public class AopProxyFactory {
7+
8+
public AopProxy createAopProxy(AdvisedSupport config){
9+
return null;
10+
}
11+
}

0 commit comments

Comments
 (0)