forked from code4craft/tiny-spring
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
428e4c7
commit 1b091c5
Showing
6 changed files
with
95 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,22 @@ | ||
tiny-spring | ||
======= | ||
|
||
A tiny IoC container refer to Spring. | ||
>A tiny IoC container refer to Spring. | ||
## 关于 | ||
|
||
`tiny-spring`是为了学习Spring的而开发的,可以认为是一个Spring的精简版。Spring的代码很多,层次复杂,阅读起来费劲。我尝试从使用功能的角度出发,参考Spring的实现,一步一步构建,最终完成一个精简版的Spring。 | ||
|
||
## 功能 | ||
|
||
1. 支持singleton类型的bean,包括初始化、属性注入、以及依赖bean注入。 | ||
2. 可从xml中读取配置。 | ||
3. 可以使用Aspectj的方式进行AOP编写,支持接口和类代理。 | ||
|
||
## 使用 | ||
|
||
`tiny-spring`是逐步进行构建的,里程碑版本我都使用了git tag来管理。例如,最开始的tag是`step-1-container-register-and-get`,那么可以使用 | ||
|
||
git checkout step-1-container-register-and-get | ||
|
||
来获得这一版本。版本历史见`changelog.md`。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,61 @@ | ||
tiny-spring | ||
===== | ||
|
||
步骤: | ||
# 第一部分:IoC容器 | ||
|
||
## 1.step1-最基本的容器 | ||
*step-1-container-register-and-get* | ||
|
||
git checkout step-1-container-register-and-get | ||
|
||
IoC最基本的角色有两个:容器(`BeanFactory`)和Bean本身。这里使用`BeanDefinition`来封装了bean对象,这样可以保存一些额外的元信息。测试代码: | ||
|
||
```java | ||
// 1.初始化beanfactory | ||
BeanFactory beanFactory = new BeanFactory(); | ||
|
||
// 2.注入bean | ||
BeanDefinition beanDefinition = new BeanDefinition(new HelloWorldService()); | ||
beanFactory.registerBeanDefinition("helloWorldService", beanDefinition); | ||
|
||
单纯的map,有get和put bean的功能 | ||
// 3.获取bean | ||
HelloWorldService helloWorldService = (HelloWorldService) beanFactory.getBean("helloWorldService"); | ||
helloWorldService.helloWorld(); | ||
``` | ||
|
||
## 2.step2-将bean创建放入工厂 | ||
*step-2-abstract-beanfactory-and-do-bean-initilizing-in-it* | ||
|
||
1. 抽象beanfactory | ||
2. 将bean初始化放入beanfactory | ||
git checkout step-2-abstract-beanfactory-and-do-bean-initilizing-in-it | ||
|
||
step1中的bean是初始化好之后再set进去的,实际使用中,我们希望容器来管理bean的创建。于是我们将bean的初始化放入BeanFactory中。为了保证扩展性,我们使用Extract Interface的方法,将`BeanFactory`替换成接口,而使用`AbstractBeanFactory`和`AutowireCapableBeanFactory`作为其实现。"AutowireCapable"的意思是“可自动装配的”,为我们后面注入属性做准备。 | ||
|
||
```java | ||
// 1.初始化beanfactory | ||
BeanFactory beanFactory = new AutowireCapableBeanFactory(); | ||
|
||
// 2.注入bean | ||
BeanDefinition beanDefinition = new BeanDefinition(); | ||
beanDefinition.setBeanClassName("us.codecraft.tinyioc.HelloWorldService"); | ||
beanFactory.registerBeanDefinition("helloWorldService", beanDefinition); | ||
|
||
// 3.获取bean | ||
HelloWorldService helloWorldService = (HelloWorldService) beanFactory.getBean("helloWorldService"); | ||
helloWorldService.helloWorld(); | ||
``` | ||
|
||
## 3.step3-为bean注入属性 | ||
*step-3-inject-bean-with-property* | ||
|
||
git checkout step-3-inject-bean-with-property | ||
|
||
## 4.step4-读取xml配置来初始化bean | ||
*step-4-config-beanfactory-with-xml* | ||
|
||
git checkout step-4-config-beanfactory-with-xml | ||
|
||
## 5.step5-为bean注入bean | ||
*step-5-inject-bean-to-bean* | ||
|
||
git checkout step-5-inject-bean-to-bean | ||
|
||
## 6.step6-ApplicationContext登场 | ||
*step-6-invite-application-context* | ||
|
||
git checkout step-6-invite-application-context | ||
|
||
# AOP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package us.codecraft.tinyioc.aop; | ||
|
||
/** | ||
* @author [email protected] | ||
*/ | ||
public class AdvisedSupport { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package us.codecraft.tinyioc.aop; | ||
|
||
/** | ||
* @author [email protected] | ||
*/ | ||
public interface AopProxy { | ||
|
||
Object getProxy(); | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/us/codecraft/tinyioc/aop/AopProxyFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package us.codecraft.tinyioc.aop; | ||
|
||
/** | ||
* @author [email protected] | ||
*/ | ||
public class AopProxyFactory { | ||
|
||
public AopProxy createAopProxy(AdvisedSupport config){ | ||
return null; | ||
} | ||
} |