Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1493e8b

Browse filesBrowse files
committed
Merge branch 'master' of github.com:coderbruis/JavaSourceLearning
2 parents 05e92b5 + 7ce6018 commit 1493e8b
Copy full SHA for 1493e8b
Expand file treeCollapse file tree

5 files changed

+63
-48
lines changed
Open diff view settings
Collapse file

‎README.md‎

Copy file name to clipboardExpand all lines: README.md
+21-10Lines changed: 21 additions & 10 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,30 @@
2222

2323
Java流行框架源码分析,学习以及总结。
2424

25+
对于框架底层源码的学习,需要反复、认真思考,并做到温故而知新,这样才能将底层原理吸收得更加牢固。
26+
2527
框架包括:
2628

2729

28-
- [x] Spring源码
29-
- [x] SpringBoot源码
30-
- [x] SpringAOP源码
31-
- [x] SpringSecurity源码
32-
- [x] SpringSecurity OAuth2源码
33-
- [x] JDK源码
34-
- [ ] Netty源码
35-
- [ ] MyBatis源码
36-
- [x] Dubbo源码
37-
- [ ] SpringCloud源码
30+
✅ Spring源码
31+
32+
✅ SpringBoot源码
33+
34+
✅ SpringAOP源码
35+
36+
✅ SpringSecurity源码
37+
38+
✅ SpringSecurity OAuth2源码
39+
40+
✅ JDK源码
41+
42+
✅ Dubbo源码
43+
44+
Netty源码
45+
46+
MyBatis源码
47+
48+
SpringCloud源码
3849

3950
> 为什么要分析、学习源码?
4051
Collapse file

‎Spring-Netty/pom.xml‎

Copy file name to clipboardExpand all lines: Spring-Netty/pom.xml
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
<properties>
1818
<java.version>1.8</java.version>
19-
<netty-all.version>4.1.6.Final</netty-all.version>
19+
<netty-all.version>4.1.42.Final</netty-all.version>
2020
</properties>
2121

2222
<dependencies>
Collapse file

‎note/JDK/深入学习String源码与底层(二).md‎

Copy file name to clipboardExpand all lines: note/JDK/深入学习String源码与底层(二).md
+2-2Lines changed: 2 additions & 2 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ false
120120
return len1 - len2;
121121
}
122122
```
123-
从compareTo()的源码可知,这方法时先比较两个字符串内的字符串数组的ASCII值,如果最小字符串都比较完了都还是相等的,则返回字符串长度的差值;否则在最小字符串比较完之前,字符不相等,则返回不相等字符的ASCII值。这里本人也有点困惑这个方法的有什么实际的用处,有了解的小伙伴可以留言,大家互相学习。
123+
从compareTo()的源码可知,这方法时先比较两个字符串内的字符串数组的ASCII值,如果最小字符串都比较完了都还是相等的,则返回字符串长度的差值;否则在最小字符串比较完之前,字符不相等,则返回不相等字符的ASCII值差值。这里本人也有点困惑这个方法的有什么实际的用处,有了解的小伙伴可以留言,大家互相学习。
124124

125125
### 5. String的startWith(String prefix)方法
126126

@@ -548,4 +548,4 @@ if (!limited || list.size() < limit)
548548

549549
### 总结
550550

551-
String作为Java中使用频率最多的类,它在日程开发中起到了至关重要的作用。由于String方法还有很多,这里就不一一总结了。
551+
String作为Java中使用频率最多的类,它在日程开发中起到了至关重要的作用。由于String方法还有很多,这里就不一一总结了。
Collapse file

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

Copy file name to clipboardExpand all lines: note/Spring/深入Spring源码系列(二)——深入Spring容器,通过源码阅读和时序图来彻底弄懂Spring容器(上).md
+32-32Lines changed: 32 additions & 32 deletions
  • Display the source diff
  • Display the rich diff
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-
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ro7s1PeW-1596181500749)(https://note.youdao.com/yws/api/personal/file/A91C9C5BB33B48A4B501435C157FFD99?method=download&shareKey=2b9a7ef7fd42d051fec83fe3f5eef7a8)]
31+
![图片1](https://note.youdao.com/yws/api/personal/file/A91C9C5BB33B48A4B501435C157FFD99?method=download&shareKey=2b9a7ef7fd42d051fec83fe3f5eef7a8)
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-
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-slboUPHM-1596181500755)(https://note.youdao.com/yws/api/personal/file/477FF4A409A94CBB8CF9A05A16D8F7D4?method=download&shareKey=de7d24b623d4c5bb7e65bb440438e271)]
51+
![图片2](https://note.youdao.com/yws/api/personal/file/477FF4A409A94CBB8CF9A05A16D8F7D4?method=download&shareKey=de7d24b623d4c5bb7e65bb440438e271)
5252

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

9595

9696

97-
**==前方高能==**
97+
** 前方高能 **
9898

99-
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZBUV9DEW-1596181500779)(https://note.youdao.com/yws/api/personal/file/219238FD61C146C99E137E303D52EA66?method=download&shareKey=d5e5aaa1e9fa782eeb056b89119c3565)]
99+
![图片3](https://note.youdao.com/yws/api/personal/file/219238FD61C146C99E137E303D52EA66?method=download&shareKey=d5e5aaa1e9fa782eeb056b89119c3565)
100100

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

103-
==1== SpringMain.class
103+
1. SpringMain.class
104104
```
105105
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
106106
```
107-
==2== ClassPathXmlApplicationContext.class
107+
2. ClassPathXmlApplicationContext.class
108108
```
109109
public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
110110
this(new String[]{configLocation}, true, (ApplicationContext)null);
@@ -122,7 +122,7 @@ public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh,
122122
}
123123
}
124124
```
125-
==3== AbstractRefreshableConfigApplicationContext.class
125+
3. AbstractRefreshableConfigApplicationContext.class
126126
```
127127
//给configLocations字符串数组设置值,支持多个配置文件已数组方式同时传入。
128128
public void setConfigLocations(String... locations) {
@@ -146,9 +146,9 @@ public void setConfigLocations(String... locations) {
146146

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

149-
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-i7GGvXbS-1596181500788)(https://note.youdao.com/yws/api/personal/file/76AE8FEDAFF54B6881C336B056AC5B0A?method=download&shareKey=430f5263180efd8467df6e6434456f3d)]
149+
![Image](https://note.youdao.com/yws/api/personal/file/76AE8FEDAFF54B6881C336B056AC5B0A?method=download&shareKey=430f5263180efd8467df6e6434456f3d)
150150

151-
==1== AbstractApplicationContext.class
151+
1. AbstractApplicationContext.class
152152
```
153153
/*
154154
简单来说,Spring容器的初始化时右refresh()方法来启动的,这个方法标志着IOC容器的正式启动。具体来说,这里的启动包括了BeanDefinition和Resource的定位、载入和注册三个基本过程。
@@ -197,7 +197,7 @@ public void refresh() throws BeansException, IllegalStateException {
197197
}
198198
}
199199
```
200-
==2== AbstractRefreshableApplicationContext.class
200+
2. AbstractRefreshableApplicationContext.class
201201
```
202202
/*
203203
通知子类刷新内部bean工厂,初始化BeanFactory并进行XML文件的解析、读取。obtain就是指获得的含义,这个方法obtaiinFreshBeanFactory正是实现BeanFactory的地方,也就是经过这个方法,ApplicationContext就已经拥有了BeanFactory的全部功能(也就是BeanFactory包含在了Spring容器里了)。
@@ -239,13 +239,13 @@ protected final void refreshBeanFactory() throws BeansException {
239239
}
240240
```
241241
这里先看看上面代码的loadBeanDefinitions()方法运行完后的结果
242-
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rct2oyUr-1596181500797)(https://note.youdao.com/yws/api/personal/file/59FBCD3CC1B54136A05309EA6B88FEB3?method=download&shareKey=80bdcfcbde0362b73eb633390c5b1042)]
243-
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GQGdWL16-1596181500806)(https://note.youdao.com/yws/api/personal/file/E258907852284A6F93A2C305319EBB64?method=download&shareKey=7e1dba96d3b53ca9b6af017552f8fd31)]
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)
244244
从图中可以知道,loadBeanDefinitions()方法运行完后,在beanFactory变量里面存放着一个ConcurrentHashMap变量,用于存放着person这个KV键值对,Key为person,Value为一个ArrayList的变量,里面存放着person的两个属性:age、name。
245245

246246
那么,person的属性是怎么被封装到beanFactory里面的呢?请看下面的源码解析。
247247

248-
==3== AbstractXmlApplicationContext.class
248+
3. AbstractXmlApplicationContext.class
249249
```
250250
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
251251
//为给定的BeanFactory创建一个新的XmlBeanDefinitionReader
@@ -271,7 +271,7 @@ protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansE
271271
```
272272
首先在refreshBeanFactory()方法中已经初始化了DefaultListableBeanFactory,对于读取XML配置文件,还需要使用XmlBeanDefinitionReader。所以在上述loadBeanDefinitions()中就需要初始化XmlBeanDefinitionReader。在DefaultListableBeanFactory和XmlBeanDefinitionReader后就可以进行配置文件的读取了。要注意的地方时,在XmlBeanDefinitionReader初始化时就已经把DefaultListableBeanFactory给注册进去了,所以在XmlBeanDefinitionReader读取的BeanDefinition都会注册到DefaultListableBeanFactory中,也就是经过上述的loadingBeanDefinitions(),类型DefaultListableBeanFactory的变量beanFactory就已经包含了所有**解析好的配置**了。
273273

274-
==4== AbstractBeanDefinitionReader.class
274+
4. AbstractBeanDefinitionReader.class
275275
```
276276
@Override
277277
public int loadBeanDefinitions(String... locations) throws BeanDefinitionStoreException {
@@ -320,7 +320,7 @@ public int loadBeanDefinitions(String location, @Nullable Set<Resource> actualRe
320320
return count;
321321
}
322322
```
323-
==5== PathMatchingResourcePatternResolver.class
323+
5. PathMatchingResourcePatternResolver.class
324324
```
325325
@Override
326326
public Resource[] getResources(String locationPattern) throws IOException {
@@ -351,7 +351,7 @@ public Resource[] getResources(String locationPattern) throws IOException {
351351
}
352352
}
353353
```
354-
==6== XmlBeanDefinitionReader.class
354+
6. XmlBeanDefinitionReader.class
355355
```
356356
/*
357357
从XML配置文件中获取bean定义信息
@@ -409,9 +409,9 @@ protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)
409409

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

412-
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-F4jy1nu2-1596181500811)(https://note.youdao.com/yws/api/personal/file/861658D89B0D4B48A7ED56B554CF3028?method=download&shareKey=c3bc974e751495bac74d9ac9ec56cb75)]
412+
![图片](https://note.youdao.com/yws/api/personal/file/861658D89B0D4B48A7ED56B554CF3028?method=download&shareKey=c3bc974e751495bac74d9ac9ec56cb75)
413413

414-
==1== XmlBeanDefinitionReader.class
414+
1. XmlBeanDefinitionReader.class
415415
```
416416
/*
417417
注册给定DOM文档中包含的bean定义
@@ -423,7 +423,7 @@ public int registerBeanDefinitions(Document doc, Resource resource) throws BeanD
423423
return this.getRegistry().getBeanDefinitionCount() - countBefore;
424424
}
425425
```
426-
==2== DefaultBeanDefinitionDocumentReader.class
426+
2. DefaultBeanDefinitionDocumentReader.class
427427
```
428428
/*
429429
此实现根据“spring-beans”XSD解析bean定义
@@ -506,7 +506,7 @@ protected void processBeanDefinition(Element ele, BeanDefinitionParserDelegate d
506506
507507
```
508508

509-
==2== BeanDefinitionParserDelegate.class
509+
2. BeanDefinitionParserDelegate.class
510510
```
511511
/*
512512
解析bean定义本身,而不考虑名称或别名,如果解析期间出错则返回null。
@@ -636,8 +636,8 @@ public void parsePropertyElement(Element ele, BeanDefinition bd) {
636636
}
637637
638638
```
639-
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GY6XA8St-1596181500816)(https://note.youdao.com/yws/api/personal/file/75CAC9D21AD64BAB89B0D25C8BBE7598?method=download&shareKey=89e73cf46fe18b1b85aecf8d58006f8e)]
640-
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CoQwCoX9-1596181500828)(https://note.youdao.com/yws/api/personal/file/CF65BB80EB934EBEBA49466CFAB261A0?method=download&shareKey=8b9f0078cf5a3171dfd69d00d9ba55f6)]
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)
641641

642642
然后,就会一路返回到refresh()方法里的加载bean定义信息的方法——loadBeanDefinitions(),此时beanFactory里面就会存在一个带有KV对的ConcurrentHashMap,而这个beanFactory会存放在Spring容器里面。
643643
```
@@ -648,8 +648,8 @@ customizeBeanFactory(beanFactory);
648648
loadBeanDefinitions(beanFactory);
649649
```
650650
再看看DefaultListableBeanFactory里面的内容
651-
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-U1YLvZqH-1596181500835)(https://note.youdao.com/yws/api/personal/file/59FBCD3CC1B54136A05309EA6B88FEB3?method=download&shareKey=80bdcfcbde0362b73eb633390c5b1042)]
652-
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Sm1vEEPj-1596181500841)(https://note.youdao.com/yws/api/personal/file/E258907852284A6F93A2C305319EBB64?method=download&shareKey=7e1dba96d3b53ca9b6af017552f8fd31)]
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)
653653

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

672672
下面先看看创建bean和初始化bean的时序图。
673-
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-d72Brshx-1596181500848)(https://note.youdao.com/yws/api/personal/file/8B415614A97D45B481925159264C344F?method=download&shareKey=1083828cfcea581b0aa5cae56e3f3090)]
673+
![Images](https://note.youdao.com/yws/api/personal/file/8B415614A97D45B481925159264C344F?method=download&shareKey=1083828cfcea581b0aa5cae56e3f3090)
674674

675-
==1== AbstractApplicationContext.class
675+
1. AbstractApplicationContext.class
676676
```
677677
public void refresh() throws BeansException, IllegalStateException {
678678
...
@@ -718,7 +718,7 @@ protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory b
718718
```
719719
这里的懒加载的意思,指的是bean单例不是在Spring容器初始化的时候就创建的,而是在要使用该bean的时候,才会创建该bean。
720720

721-
==2== DefaultListableBeanFactory.class
721+
2. DefaultListableBeanFactory.class
722722
```
723723
// 实例剩余的(非懒加载)的单例
724724
@Override
@@ -779,7 +779,7 @@ public void preInstantiateSingletons() throws BeansException {
779779
}
780780
```
781781

782-
==3== AbstractBeanFactory.class
782+
3. AbstractBeanFactory.class
783783
```
784784
protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType,
785785
@Nullable final Object[] args, boolean typeCheckOnly) throws BeansException {
@@ -894,7 +894,7 @@ protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredTy
894894
}
895895
```
896896

897-
==4== DefaultSingletonBeanRegistry.class
897+
4. DefaultSingletonBeanRegistry.class
898898
```
899899
/*
900900
尝试从缓存中获取单例对象,如果缓存中有该单例对象,并且该对象正在被创建,则从缓存中获取。
@@ -967,9 +967,9 @@ public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
967967
```
968968

969969
无图无真相:
970-
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Fbdqjrix-1596181500853)(https://note.youdao.com/yws/api/personal/file/4C30C0DA143E422FBD27E50AE71AC179?method=download&shareKey=2f4dff65df0e9761ede47d26782dd977)]
970+
![Images](https://note.youdao.com/yws/api/personal/file/4C30C0DA143E422FBD27E50AE71AC179?method=download&shareKey=2f4dff65df0e9761ede47d26782dd977)
971971

972-
==5== AbstractAutowireCapableBeanFactory.class
972+
5. AbstractAutowireCapableBeanFactory.class
973973
```
974974
/*
975975
该类的中心方法:创建bean实例,实例化bean实例,应用bean的后置处理器
@@ -1253,7 +1253,7 @@ protected void applyPropertyValues(String beanName, BeanDefinition mbd, BeanWrap
12531253
经过上面的分析,就知道真正的对bean赋值填充是在AbstractAutowireCapableBeanFactory.class类里的applyPropertyValues方法里的,并且是通过对原属性值进行了一次深拷贝,然后将深拷贝后的属性值填充到bean里的。
12541254

12551255
## 在web容器中初始化spring容器
1256-
[深入Spring源码系列(二)——深入Spring容器,通过源码阅读和时序图来彻底弄懂Spring容器(下)](https://blog.csdn.net/CoderBruis/article/details/86505582)
1256+
[深入Spring源码系列(二)——深入Spring容器,通过源码阅读和时序图来彻底弄懂Spring容器(下)](https://github.com/coderbruis/JavaSourceCodeLearning/blob/master/note/Spring/%E6%B7%B1%E5%85%A5Spring%E6%BA%90%E7%A0%81%E7%B3%BB%E5%88%97%EF%BC%88%E4%BA%8C%EF%BC%89%E2%80%94%E2%80%94%E6%B7%B1%E5%85%A5Spring%E5%AE%B9%E5%99%A8%EF%BC%8C%E9%80%9A%E8%BF%87%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E5%92%8C%E6%97%B6%E5%BA%8F%E5%9B%BE%E6%9D%A5%E5%BD%BB%E5%BA%95%E5%BC%84%E6%87%82Spring%E5%AE%B9%E5%99%A8%EF%BC%88%E4%B8%8B%EF%BC%89.md)
12571257

12581258

12591259
## 参考

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.