@@ -511,9 +511,13 @@ protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable O
511
511
// Make sure bean class is actually resolved at this point, and
512
512
// clone the bean definition in case of a dynamically resolved Class
513
513
// which cannot be stored in the shared merged bean definition.
514
+ /**
515
+ * 去加在我们beanName对应的那个class
516
+ */
514
517
Class <?> resolvedClass = resolveBeanClass (mbd , beanName );
515
518
if (resolvedClass != null && !mbd .hasBeanClass () && mbd .getBeanClassName () != null ) {
516
519
mbdToUse = new RootBeanDefinition (mbd );
520
+ // 这个时候把加载好的class设置到beanClass里面,表示已经加载完了,下一次就不会在需要去加载class了
517
521
mbdToUse .setBeanClass (resolvedClass );
518
522
}
519
523
@@ -526,7 +530,17 @@ protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable O
526
530
beanName , "Validation of method overrides failed" , ex );
527
531
}
528
532
533
+ // ------------------------
534
+ // 加载好了bean,开始进行实例化
535
+
529
536
try {
537
+ /**
538
+ * 在实例化之前的处理,这里有一个特殊的BeanPostProcessor叫做InstantiationAwareBeanPostProcessor
539
+ * 这个BeanPostProcessor可以让我们使用我们自己的方式去newInstance一个bean,而不是让spring我们调用构造方法去new bean对象
540
+ * 所以在这里面会判断系统是否有InstantiationAwareBeanPostProcessor,如果有则循环调用这些PostProcessor然后去调用方法
541
+ * 如果这个方法返回的非null,则表达使用了这个InstantiationAwareBeanPostProcessor去new对象,也就直接返回了不会走后面spring的流程
542
+ * 在这个流程中也会走到BeanPostProcessor的after进行一些AOP操作
543
+ */
530
544
// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
531
545
Object bean = resolveBeforeInstantiation (beanName , mbdToUse );
532
546
if (bean != null ) {
@@ -539,6 +553,7 @@ protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable O
539
553
}
540
554
541
555
try {
556
+ // 如果上面没有走自定义的New bean方法,那么就走spring的实例化
542
557
Object beanInstance = doCreateBean (beanName , mbdToUse , args );
543
558
if (logger .isTraceEnabled ()) {
544
559
logger .trace ("Finished creating instance of bean '" + beanName + "'" );
@@ -578,16 +593,21 @@ protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable
578
593
if (mbd .isSingleton ()) {
579
594
instanceWrapper = this .factoryBeanInstanceCache .remove (beanName );
580
595
}
596
+ // 一般流程会走下面的逻辑,创建bean的实例,会走推断构造方法逻辑
597
+ // 实例化
581
598
if (instanceWrapper == null ) {
582
599
instanceWrapper = createBeanInstance (beanName , mbd , args );
583
600
}
601
+ // 拿到我们的bean对象
584
602
Object bean = instanceWrapper .getWrappedInstance ();
585
603
Class <?> beanType = instanceWrapper .getWrappedClass ();
586
604
if (beanType != NullBean .class ) {
587
605
mbd .resolvedTargetType = beanType ;
588
606
}
589
607
590
608
// Allow post-processors to modify the merged bean definition.
609
+ // 当上面bean实例化之后,会判断系统中是否包含MergedBeanDefinitionPostProcessor列表,如果包含了则会调用这个对象中的
610
+ // postProcessMergedBeanDefinition方法
591
611
synchronized (mbd .postProcessingLock ) {
592
612
if (!mbd .postProcessed ) {
593
613
try {
@@ -603,6 +623,7 @@ protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable
603
623
604
624
// Eagerly cache singletons to be able to resolve circular references
605
625
// even when triggered by lifecycle interfaces like BeanFactoryAware.
626
+ // 处理循环依赖的问题
606
627
boolean earlySingletonExposure = (mbd .isSingleton () && this .allowCircularReferences &&
607
628
isSingletonCurrentlyInCreation (beanName ));
608
629
if (earlySingletonExposure ) {
@@ -616,7 +637,9 @@ protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable
616
637
// Initialize the bean instance.
617
638
Object exposedObject = bean ;
618
639
try {
640
+ // 属性填充
619
641
populateBean (beanName , mbd , instanceWrapper );
642
+ // 前面是实例化也就是new,现在是对bean进行初始化
620
643
exposedObject = initializeBean (beanName , exposedObject , mbd );
621
644
}
622
645
catch (Throwable ex ) {
@@ -658,6 +681,7 @@ else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {
658
681
659
682
// Register bean as disposable.
660
683
try {
684
+ // 初始化完成之后,需要做一些为销毁bean的准备
661
685
registerDisposableBeanIfNecessary (beanName , bean , mbd );
662
686
}
663
687
catch (BeanDefinitionValidationException ex ) {
@@ -1127,13 +1151,15 @@ protected void applyMergedBeanDefinitionPostProcessors(RootBeanDefinition mbd, C
1127
1151
@ Nullable
1128
1152
protected Object resolveBeforeInstantiation (String beanName , RootBeanDefinition mbd ) {
1129
1153
Object bean = null ;
1130
- if (!Boolean .FALSE .equals (mbd .beforeInstantiationResolved )) {
1154
+ if (!Boolean .FALSE .equals (mbd .beforeInstantiationResolved )) { // 已经使用了InstantiationAwareBeanPostProcessor初始化了,就不需要在初始化了
1131
1155
// Make sure bean class is actually resolved at this point.
1156
+ // 这个bean不是一个合成的bean对象如果是的话不需要创建bean,并且系统中包含了InstantiationAwareBeanPostProcessor的Bean
1132
1157
if (!mbd .isSynthetic () && hasInstantiationAwareBeanPostProcessors ()) {
1133
1158
Class <?> targetType = determineTargetType (beanName , mbd );
1134
1159
if (targetType != null ) {
1160
+ // 循环所有的InstantiationAwareBeanPostProcessor进行before,如果返回不为空说明使用自定义的创建bean的方法
1135
1161
bean = applyBeanPostProcessorsBeforeInstantiation (targetType , beanName );
1136
- if (bean != null ) {
1162
+ if (bean != null ) { // 使用了自定义创建Bean的方法也要走完完整的BeanPostProcessor的after,用来AOP操作
1137
1163
bean = applyBeanPostProcessorsAfterInitialization (bean , beanName );
1138
1164
}
1139
1165
}
@@ -1156,6 +1182,8 @@ protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition
1156
1182
*/
1157
1183
@ Nullable
1158
1184
protected Object applyBeanPostProcessorsBeforeInstantiation (Class <?> beanClass , String beanName ) {
1185
+ // 循环InstantiationAwareBeanPostProcessor,进行调用判断是否有返回值,第一个有返回值的就直接返回了
1186
+ // 所以如果有多个InstantiationAwareBeanPostProcessor都可以返回,则只会使用第一个
1159
1187
for (InstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache ().instantiationAware ) {
1160
1188
Object result = bp .postProcessBeforeInstantiation (beanClass , beanName );
1161
1189
if (result != null ) {
@@ -1395,6 +1423,9 @@ protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable B
1395
1423
// Give any InstantiationAwareBeanPostProcessors the opportunity to modify the
1396
1424
// state of the bean before properties are set. This can be used, for example,
1397
1425
// to support styles of field injection.
1426
+ // 一个非合成的正常的bean对象,判断系统中是否有InstantiationAwareBeanPostProcessor的BeanPostProcessor
1427
+ // 如果存在则调用它的postProcessAfterInstantiation方法
1428
+ // 所以这个流程也就是调用在实例化之后,属性填充和初始化流程之前
1398
1429
if (!mbd .isSynthetic () && hasInstantiationAwareBeanPostProcessors ()) {
1399
1430
for (InstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache ().instantiationAware ) {
1400
1431
if (!bp .postProcessAfterInstantiation (bw .getWrappedInstance (), beanName )) {
@@ -1404,7 +1435,15 @@ protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable B
1404
1435
}
1405
1436
1406
1437
PropertyValues pvs = (mbd .hasPropertyValues () ? mbd .getPropertyValues () : null );
1407
-
1438
+ /**
1439
+ * 这段流程是兼容的历史的版本,spring现在已经不这么推荐我们去注入属性的时候使用这种方式了
1440
+ * 当我们在xml或者在@Bean中,可以设置一个属性叫做autowired,可以通过这种设置来让我们创建bean填充里面的属性的时候使用byName或者byType的方式
1441
+ * 比如我们的A bean中有一个属性是B bean我们需要创建一个setB的方法
1442
+ * 如果我们使用byType,spring就会去找B这个bean给我们自动赋值到b这个属性上
1443
+ * 如果我们使用了byName的方式,就会去找setB这个方法上的B(注意是方法名称截取set),去找对应的bean设置到属性上
1444
+ * 也就是如果我们设置了autowire属性,spring就会找你bean里面所有的set方法,然后进行赋值调用
1445
+ * 但是因为这个已经不推荐了,所以还是推荐使用类似@Autowired的方式或者构造方式来进行属性赋值
1446
+ */
1408
1447
int resolvedAutowireMode = mbd .getResolvedAutowireMode ();
1409
1448
if (resolvedAutowireMode == AUTOWIRE_BY_NAME || resolvedAutowireMode == AUTOWIRE_BY_TYPE ) {
1410
1449
MutablePropertyValues newPvs = new MutablePropertyValues (pvs );
@@ -1419,14 +1458,21 @@ protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable B
1419
1458
pvs = newPvs ;
1420
1459
}
1421
1460
1461
+ // 继续判断是否存在InstantiationAwareBeanPostProcessor的bean
1422
1462
boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors ();
1423
1463
boolean needsDepCheck = (mbd .getDependencyCheck () != AbstractBeanDefinition .DEPENDENCY_CHECK_NONE );
1424
1464
1425
1465
PropertyDescriptor [] filteredPds = null ;
1426
- if (hasInstAwareBpps ) {
1466
+ if (hasInstAwareBpps ) { // 如果存在InstantiationAwareBeanPostProcessor的bean
1467
+ // 这个pvs代表的是是否已经在我们的BeanDefinition里面提前给一些属性赋值了
1468
+ // 比如我们的A bean里面的B属性,如果提前在PropertyValues里面设置了,则下面的@Autowired注解啊什么的,就不会再次去赋值了
1469
+ // 提前设置PropertyValues可以在我们前面的MergedBeanDefinitionPostProcessor的时候,对BeanDefinition进行设置
1427
1470
if (pvs == null ) {
1428
1471
pvs = mbd .getPropertyValues ();
1429
1472
}
1473
+ // 循环调用InstantiationAwareBeanPostProcessor里面的postProcessProperties方法
1474
+ // 需要注意的是我们的@Autowired, @Resource @Inject就是在这个流程中实现的
1475
+ // 所以这里主要是完成了我们Spring的一个依赖注入功能的地方
1430
1476
for (InstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache ().instantiationAware ) {
1431
1477
PropertyValues pvsToUse = bp .postProcessProperties (pvs , bw .getWrappedInstance (), beanName );
1432
1478
if (pvsToUse == null ) {
@@ -1448,7 +1494,7 @@ protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable B
1448
1494
checkDependencies (beanName , mbd , filteredPds , pvs );
1449
1495
}
1450
1496
1451
- if (pvs != null ) {
1497
+ if (pvs != null ) { // 就是在这里把我们自定义的一些propertyValue设置到bean里面
1452
1498
applyPropertyValues (beanName , mbd , bw , pvs );
1453
1499
}
1454
1500
}
@@ -1788,15 +1834,26 @@ protected Object initializeBean(String beanName, Object bean, @Nullable RootBean
1788
1834
}, getAccessControlContext ());
1789
1835
}
1790
1836
else {
1837
+ // 调用一下aware的方法:BeanNameAware、BeanClassLoaderAware、BeanFactoryAware
1838
+ // 主要是beanNameAware,如果我们一个bean想要知道自己的beanName就通过实现BeanNameAware来拿到
1791
1839
invokeAwareMethods (beanName , bean );
1792
1840
}
1793
1841
1794
1842
Object wrappedBean = bean ;
1843
+ // 初始化之前调用我们BeanPostProcessor的before方法,可能会在里面改变我们的bean对象
1844
+ /**
1845
+ * 主要在意两个
1846
+ * 一个是ApplicationContextAwareProcessor这个对象,用来实现我们各种aware回调的
1847
+ * 比如我们想要获取ApplicationContext去实现ApplicationContextAware,就在这里回调的
1848
+ * 第二个是InitDestroyAnnotationBeanPostProcessor这个,是用来处理我们@PostContrscut注解的
1849
+ * 也就是我们在bean中使用的这个注解会在这里被调用
1850
+ */
1795
1851
if (mbd == null || !mbd .isSynthetic ()) {
1796
1852
wrappedBean = applyBeanPostProcessorsBeforeInitialization (wrappedBean , beanName );
1797
1853
}
1798
1854
1799
1855
try {
1856
+ // 调用初始化方法
1800
1857
invokeInitMethods (beanName , wrappedBean , mbd );
1801
1858
}
1802
1859
catch (Throwable ex ) {
@@ -1805,6 +1862,7 @@ protected Object initializeBean(String beanName, Object bean, @Nullable RootBean
1805
1862
beanName , "Invocation of init method failed" , ex );
1806
1863
}
1807
1864
if (mbd == null || !mbd .isSynthetic ()) {
1865
+ // 初始化之后调用我们BeanPostProcessor的after方法,AOP流程主要在这里实现
1808
1866
wrappedBean = applyBeanPostProcessorsAfterInitialization (wrappedBean , beanName );
1809
1867
}
1810
1868
@@ -1843,6 +1901,7 @@ private void invokeAwareMethods(String beanName, Object bean) {
1843
1901
protected void invokeInitMethods (String beanName , Object bean , @ Nullable RootBeanDefinition mbd )
1844
1902
throws Throwable {
1845
1903
1904
+ // 如果我们的bean实现了InitializingBean,则调用它的afterPropertiesSet方法
1846
1905
boolean isInitializingBean = (bean instanceof InitializingBean );
1847
1906
if (isInitializingBean && (mbd == null || !mbd .hasAnyExternallyManagedInitMethod ("afterPropertiesSet" ))) {
1848
1907
if (logger .isTraceEnabled ()) {
@@ -1864,6 +1923,7 @@ protected void invokeInitMethods(String beanName, Object bean, @Nullable RootBea
1864
1923
}
1865
1924
}
1866
1925
1926
+ // 如果我们的beanDefinition里面设置了initMethodName,则还会调用这个方法
1867
1927
if (mbd != null && bean .getClass () != NullBean .class ) {
1868
1928
String initMethodName = mbd .getInitMethodName ();
1869
1929
if (StringUtils .hasLength (initMethodName ) &&
0 commit comments