Spring---循环依赖-@Autowired

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
     *  【循环依赖---@Autowired】
     *
     *      BeanPostProcessor : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor
     *
     *                              org.springframework.beans.factory.config.BeanPostProcessor implementation that autowires annotated fields, setter methods, and arbitrary config methods.
     *                              BeanPostProcessor实现,用来自动织入 注解字段...;
     *                              Such members to be injected are detected through annotations: by default, Spring's @Autowired and @Value annotations.
     *                              一些成员将被检测到:Spring的@Autowired、@Value;
     *
     *      public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter implements MergedBeanDefinitionPostProcessor, PriorityOrdered, BeanFactoryAware {
     *
     *          public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) {}
     *      }
     *
     *      1、
     *      DefaultSingletonBeanRegistry#getSingleton(String beanName){
     *          Object singletonObject = this.singletonObjects.get(beanName);
     *          if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
     *              synchronized (this.singletonObjects) {
     *                  singletonObject = this.earlySingletonObjects.get(beanName);
     *                  if (singletonObject == null && allowEarlyReference) {
     *                      ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
     *                      if (singletonFactory != null) {
     *                          singletonObject = singletonFactory.getObject();            === 2、
     *                          this.earlySingletonObjects.put(beanName, singletonObject);
     *                          this.singletonFactories.remove(beanName);
     *                      }
     *                 }
     *             }
     *        }
     *        return singletonObject;
     *      }
     *
     *      2、
     *      AbstractAutowireCapableBeanFactory#getEarlyBeanReference{
     *          Object exposedObject = bean;
     *          if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
     *              for (BeanPostProcessor bp : getBeanPostProcessors()) {
     *                  if (bp instanceof SmartInstantiationAwareBeanPostProcessor) {
     *                      SmartInstantiationAwareBeanPostProcessor ibp = (SmartInstantiationAwareBeanPostProcessor) bp;
     *                      exposedObject = ibp.getEarlyBeanReference(exposedObject, beanName);
     *                  }
     *              }
     *          }
     *          return exposedObject;
     *      }
     *
     *      3、
     *      DefaultSingletonBeanRegistry#getSingleton(String, ObjectFactory){
     *          ...
     *          singletonObject = singletonFactory.getObject();
     *          ...
     *          if (newSingleton) {
     *              addSingleton(beanName, singletonObject);  === 4、
     *          }
     *          ...
     *      }
     *
     *      4、DefaultSingletonBeanRegistry#addSingleton{
     *          synchronized (this.singletonObjects) {
     *              this.singletonObjects.put(beanName, singletonObject);
     *              this.singletonFactories.remove(beanName);
     *              this.earlySingletonObjects.remove(beanName);
     *              this.registeredSingletons.add(beanName);
     *          }
     *      }
     *
     *      5、
     *      DefaultSingletonBeanRegistry#addSingletonFactory(String beanName, ObjectFactory<?> singletonFactory){
     *          synchronized (this.singletonObjects) {
     *              if (!this.singletonObjects.containsKey(beanName)) {
     *                  this.singletonFactories.put(beanName, singletonFactory);
     *                  this.earlySingletonObjects.remove(beanName);
     *                  this.registeredSingletons.add(beanName);
     *              }
     *          }
     *      }
     *
     *
     *      实现思路:
     *
     *      AbstractBeanFactory#getBean
     *      ->
     *      AbstractBeanFactory#doGetBean{
     *          ...
     *          Object sharedInstance = getSingleton(beanName);   === 1、
     *          if (sharedInstance != null && args == null) {
     *              ...
     *          }
     *          else{
     *              ...
     *              if (mbd.isSingleton()) {
     *                  sharedInstance = getSingleton(beanName, () -> {   === 3、
     *                      return createBean(beanName, mbd, args);
     *                  }
     *                  ...
     *              }
     *              ...
     *          }
     *          ...
     *      }
     *      ->
     *      DefaultSingletonBeanRegistry#getSingleton(String, ObjectFactory) -> singletonFactory.getObject()
     *                                                                    AbstractBeanFactory#createBean <-
     *      ->
     *      AbstractAutowireCapableBeanFactory#createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args){
     *          ...
     *          Object beanInstance = doCreateBean(beanName, mbdToUse, args);
     *          ...
     *      }
     *      ->
     *      AbstractAutowireCapableBeanFactory#doCreateBean(java.lang.String, RootBeanDefinition, java.lang.Object[]){
     *          ...
     *          instanceWrapper = createBeanInstance(beanName, mbd, args);
     *          ...
     *          boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences && isSingletonCurrentlyInCreation(beanName));
     *          if (earlySingletonExposure) {
     *              addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));   === 5、
     *          }
     *          ...
     *          populateBean(beanName, mbd, instanceWrapper);
     *          ...
     *      }
     *      ->
     *      AbstractAutowireCapableBeanFactory#populateBean{
     *          ...
     *          if (hasInstAwareBpps) {
     *              for (BeanPostProcessor bp : getBeanPostProcessors()) {
     *                  ...
     *                  PropertyValues pvsToUse = ibp.postProcessProperties(pvs, bw.getWrappedInstance(), beanName);
     *                  ...
     *              }
     *          }
     *          ...
     *      }
     *      ->
     *      AutowiredAnnotationBeanPostProcessor#postProcessProperties{
     *          ...
     *          metadata.inject(bean, beanName, pvs);
     *          ...
     *      }
     *      ->
     *      InjectionMetadata#inject{
     *          ...
     *          element.inject(target, beanName, pvs);
     *          ...
     *      }
     *      ->
     *      AutowiredAnnotationBeanPostProcessor.AutowiredFieldElement#inject{
     *          ...
     *          value = beanFactory.resolveDependency(desc, beanName, autowiredBeanNames, typeConverter);
     *          ...
     *      }
     *      ->
     *      DefaultListableBeanFactory#resolveDependency{
     *          ...
     *          result = doResolveDependency(descriptor, requestingBeanName, autowiredBeanNames, typeConverter);
     *          ...
     *      }
     *      ->
     *      DefaultListableBeanFactory#doResolveDependency{
     *          ...
     *          instanceCandidate = descriptor.resolveCandidate(autowiredBeanName, type, this);
     *          ...
     *      }
     *      DependencyDescriptor#resolveCandidate{
     *          return beanFactory.getBean(beanName);
     *      }
     *      ->
     *      递归
     *
     *
     */

  

posted on   anpeiyong  阅读(290)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2020-03-23 JavaSE---进制

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示