Skip to content

Commit 85517ab

Browse files
committed
[F] spring图片
1 parent 495730f commit 85517ab

17 files changed

+15
-15
lines changed

note/Spring/深入Spring源码系列(二)——深入Spring容器,通过源码阅读和时序图来彻底弄懂Spring容器(上).md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ ApplicationContext bf = new ClassPathXmlApplicationContext("applicationContext.x
2828
### DefaultListableBeanFactory
2929
DefaultListableBeanFactory是整个bean加载的核心部分,是Spring注册及加载bean的默认实现。下面看看DefaultListableBeanFactory的层次结构图。
3030

31-
![图片1](https://note.youdao.com/yws/api/personal/file/A91C9C5BB33B48A4B501435C157FFD99?method=download&shareKey=2b9a7ef7fd42d051fec83fe3f5eef7a8)
31+
![spring-01](https://github.com/coderbruis/JavaSourceCodeLearning/blob/master/note/images/spring/spring-01.png)
3232
从上往下开始介绍各个类以及接口的作用:
3333
- AliasRegistry(接口):alias指的是bean的别名,而aliasRegistry定义了对alias的增删改查等操作。
3434
- SimpleAliasRegistry(类):主要使用map作为alias的缓存,并对接口AliasRegistry进行实现。
@@ -48,7 +48,7 @@ DefaultListableBeanFactory是整个bean加载的核心部分,是Spring注册
4848

4949
### XmlBeanDefinitionReader
5050
XML配置文件的读取是Spring中最重要的功能,因为Spring的大部分功能都是以配置作为切入点的,XmlBeanDefinitionReader实现了对资源文件的读取、解析以及注册。先看一下XmlBeanDefinitionReader的层次结构图。
51-
![图片2](https://note.youdao.com/yws/api/personal/file/477FF4A409A94CBB8CF9A05A16D8F7D4?method=download&shareKey=de7d24b623d4c5bb7e65bb440438e271)
51+
![spring-02](https://github.com/coderbruis/JavaSourceCodeLearning/blob/master/note/images/spring/spring-02.png)
5252

5353
- EnvironmentCapable(接口):定义获取Environment方法,Environment代表了配置文件。
5454
- BeanDefinitionReader(接口):主要定义资源文件读取并转换为BeanDefinition的各个功能。
@@ -96,7 +96,7 @@ Person{name='Bruis', age=23}
9696

9797
** 前方高能 **
9898

99-
![图片3](https://note.youdao.com/yws/api/personal/file/219238FD61C146C99E137E303D52EA66?method=download&shareKey=d5e5aaa1e9fa782eeb056b89119c3565)
99+
![spring-03](https://github.com/coderbruis/JavaSourceCodeLearning/blob/master/note/images/spring/spring-03.jpg)
100100

101101
通过在断点debug,跟踪程序运行。
102102

@@ -146,7 +146,7 @@ public void setConfigLocations(String... locations) {
146146

147147
下面我们来重点看看refresh()过程。
148148

149-
![Image](https://note.youdao.com/yws/api/personal/file/76AE8FEDAFF54B6881C336B056AC5B0A?method=download&shareKey=430f5263180efd8467df6e6434456f3d)
149+
![spring-04](https://github.com/coderbruis/JavaSourceCodeLearning/blob/master/note/images/spring/spring-04.jpg)
150150

151151
1. AbstractApplicationContext.class
152152
```Java
@@ -239,8 +239,8 @@ protected final void refreshBeanFactory() throws BeansException {
239239
}
240240
```
241241
这里先看看上面代码的loadBeanDefinitions()方法运行完后的结果
242-
![图片](https://note.youdao.com/yws/api/personal/file/59FBCD3CC1B54136A05309EA6B88FEB3?method=download&shareKey=80bdcfcbde0362b73eb633390c5b1042)
243-
![图片](https://note.youdao.com/yws/api/personal/file/E258907852284A6F93A2C305319EBB64?method=download&shareKey=7e1dba96d3b53ca9b6af017552f8fd31)
242+
![spring-05](https://github.com/coderbruis/JavaSourceCodeLearning/blob/master/note/images/spring/spring-05.png)
243+
![spring-06](https://github.com/coderbruis/JavaSourceCodeLearning/blob/master/note/images/spring/spring-06.png)
244244
从图中可以知道,loadBeanDefinitions()方法运行完后,在beanFactory变量里面存放着一个ConcurrentHashMap变量,用于存放着person这个KV键值对,Key为person,Value为一个ArrayList的变量,里面存放着person的两个属性:age、name。
245245

246246
那么,person的属性是怎么被封装到beanFactory里面的呢?请看下面的源码解析。
@@ -409,7 +409,7 @@ protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)
409409

410410
下面,继续深入registerBeanDefinitions方法。
411411

412-
![图片](https://note.youdao.com/yws/api/personal/file/861658D89B0D4B48A7ED56B554CF3028?method=download&shareKey=c3bc974e751495bac74d9ac9ec56cb75)
412+
![spring-07](https://github.com/coderbruis/JavaSourceCodeLearning/blob/master/note/images/spring/spring-07.jpg)
413413

414414
1. XmlBeanDefinitionReader.class
415415
```Java
@@ -636,8 +636,8 @@ public void parsePropertyElement(Element ele, BeanDefinition bd) {
636636
}
637637

638638
```
639-
![Images](https://note.youdao.com/yws/api/personal/file/75CAC9D21AD64BAB89B0D25C8BBE7598?method=download&shareKey=89e73cf46fe18b1b85aecf8d58006f8e)
640-
![Images](https://note.youdao.com/yws/api/personal/file/CF65BB80EB934EBEBA49466CFAB261A0?method=download&shareKey=8b9f0078cf5a3171dfd69d00d9ba55f6)
639+
![spring-08](https://github.com/coderbruis/JavaSourceCodeLearning/blob/master/note/images/spring/spring-08.png)
640+
![spring-09](https://github.com/coderbruis/JavaSourceCodeLearning/blob/master/note/images/spring/spring-09.png)
641641

642642
然后,就会一路返回到refresh()方法里的加载bean定义信息的方法——loadBeanDefinitions(),此时beanFactory里面就会存在一个带有KV对的ConcurrentHashMap,而这个beanFactory会存放在Spring容器里面。
643643
```Java
@@ -648,8 +648,8 @@ customizeBeanFactory(beanFactory);
648648
loadBeanDefinitions(beanFactory);
649649
```
650650
再看看DefaultListableBeanFactory里面的内容
651-
![Images](https://note.youdao.com/yws/api/personal/file/59FBCD3CC1B54136A05309EA6B88FEB3?method=download&shareKey=80bdcfcbde0362b73eb633390c5b1042)
652-
![Images](https://note.youdao.com/yws/api/personal/file/E258907852284A6F93A2C305319EBB64?method=download&shareKey=7e1dba96d3b53ca9b6af017552f8fd31)
651+
![spring-10](https://github.com/coderbruis/JavaSourceCodeLearning/blob/master/note/images/spring/spring-10.png)
652+
![spring-11](https://github.com/coderbruis/JavaSourceCodeLearning/blob/master/note/images/spring/spring-11.png)
653653

654654
上面的过程,就已经完成了Spring容器的初始化过程,相信读者也已经对Spring容器的初始化有了一个大致的了解。下面总结一下Spring容器的初始化:
655655
- 第一个过程是Resource定位过程。这个Resource定位过程指的是BeanDefinition的资源定位,它由ResourceLoader通过统一的Resource接口来完成,这个Resource对各种形式的BeanDefinition的使用都提供了统一接口。这个定位过程类似于容器寻找数据的过程,就像使用水桶装水先要把水找到一样。
@@ -670,7 +670,7 @@ bean的创建和初始化过程是在refresh方法里的invokeBeanFactoryPostPro
670670
- 当容器关闭时,调用Bean的销毁方法
671671

672672
下面先看看创建bean和初始化bean的时序图。
673-
![Images](https://note.youdao.com/yws/api/personal/file/8B415614A97D45B481925159264C344F?method=download&shareKey=1083828cfcea581b0aa5cae56e3f3090)
673+
![spring-12](https://github.com/coderbruis/JavaSourceCodeLearning/blob/master/note/images/spring/spring-12.jpg)
674674

675675
1. AbstractApplicationContext.class
676676
```Java
@@ -967,7 +967,7 @@ public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
967967
```
968968

969969
无图无真相:
970-
![Images](https://note.youdao.com/yws/api/personal/file/4C30C0DA143E422FBD27E50AE71AC179?method=download&shareKey=2f4dff65df0e9761ede47d26782dd977)
970+
![spring-13](https://github.com/coderbruis/JavaSourceCodeLearning/blob/master/note/images/spring/spring-13.png)
971971

972972
5. AbstractAutowireCapableBeanFactory.class
973973
```Java

note/Spring/深入Spring源码系列(二)——深入Spring容器,通过源码阅读和时序图来彻底弄懂Spring容器(下).md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ SpringIOC容器是如何在Web环境中被加载并起作用的?SpringIOC容
7979
IOC容器的启动过程就是建立Spring上下文的过程,该上下文是与ServletContext相伴而生的,同时也是IOC容器在Web应用环境中的具体表现之一。由ContextLoaderListener启动的上下文为根上下文。在根上下文的基础上,还有一个与Web MVC相关的上下文应用来保存控制器(DispatcherServlet)需要的MVC对象,**作为根上下文的子上下文**,构成一个层次化的上下文体系,这个与Web MVC相关的上下文——WebApplicationContext。在Web容器中启动Spring应用程序时,首先建立根上下文,然后建立这个上下文体系,这个上下文体系的建立是由ContextLoader来完成的。简单点说,ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。
8080

8181
先看看Web程序启动到SpringIOC容器创建和初始化的整个过程。
82-
![image](https://note.youdao.com/yws/api/personal/file/9755412D703C4DE287B26AF2396E57BD?method=download&shareKey=52783ceb34f405ad47140c22da34275e)
82+
![spring-14](https://github.com/coderbruis/JavaSourceCodeLearning/blob/master/note/images/spring/spring-14.jpg)
8383

8484
结合着时序图,再去调试源码,思路会清晰很多。
8585

@@ -272,7 +272,7 @@ protected Class<?> determineContextClass(ServletContext servletContext) {
272272
```
273273

274274
下面看看默认的IOC容器是什么。有图有真相:
275-
![image](https://note.youdao.com/yws/api/personal/file/AB1007BC2A7549D7898417D6231AE4E3?method=download&shareKey=e851d344aedd461f319dba3b8e2c6fe8)
275+
![spring-15](https://github.com/coderbruis/JavaSourceCodeLearning/blob/master/note/images/spring/spring-15.jpg)
276276

277277
```Java
278278
protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc) {

note/images/spring/spring-01.png

56.6 KB
Loading

note/images/spring/spring-02.png

19 KB
Loading

note/images/spring/spring-03.jpg

27.8 KB
Loading

note/images/spring/spring-04.jpg

37.9 KB
Loading

note/images/spring/spring-05.png

97.7 KB
Loading

note/images/spring/spring-06.png

79.3 KB
Loading

note/images/spring/spring-07.jpg

26.9 KB
Loading

note/images/spring/spring-08.png

157 KB
Loading

0 commit comments

Comments
 (0)