23
23
24
24
import org .springframework .aop .scope .ScopedProxyUtils ;
25
25
import org .springframework .beans .BeansException ;
26
- import org .springframework .beans .factory .BeanFactory ;
27
26
import org .springframework .beans .factory .BeanFactoryUtils ;
28
27
import org .springframework .beans .factory .FactoryBean ;
29
28
import org .springframework .beans .factory .NoUniqueBeanDefinitionException ;
44
43
45
44
/**
46
45
* A {@link BeanFactoryPostProcessor} implementation that processes identified
47
- * use of {@link BeanOverride @BeanOverride} and adapts the {@link BeanFactory}
46
+ * use of {@link BeanOverride @BeanOverride} and adapts the {@code BeanFactory}
48
47
* accordingly.
49
48
*
50
49
* <p>For each override, the bean factory is prepared according to the chosen
@@ -119,17 +118,24 @@ private void replaceOrCreateBean(ConfigurableListableBeanFactory beanFactory, Be
119
118
120
119
// NOTE: This method supports 3 distinct scenarios which must be accounted for.
121
120
//
122
- // 1) JVM runtime
123
- // 2) AOT processing
124
- // 3) AOT runtime
121
+ // - JVM runtime
122
+ // - AOT processing
123
+ // - AOT runtime
124
+ //
125
+ // In addition, this method supports 4 distinct use cases.
126
+ //
127
+ // 1) Override existing bean by-type
128
+ // 2) Create bean by-type, with a generated name
129
+ // 3) Override existing bean by-name
130
+ // 4) Create bean by-name, with a provided name
125
131
126
132
String beanName = handler .getBeanName ();
127
133
Field field = handler .getField ();
128
134
BeanDefinition existingBeanDefinition = null ;
129
135
if (beanName == null ) {
130
136
beanName = getBeanNameForType (beanFactory , handler , requireExistingBean );
131
137
if (beanName != null ) {
132
- // We are overriding an existing bean by-type.
138
+ // 1) We are overriding an existing bean by-type.
133
139
beanName = BeanFactoryUtils .transformedBeanName (beanName );
134
140
// If we are overriding a manually registered singleton, we won't find
135
141
// an existing bean definition.
@@ -138,15 +144,16 @@ private void replaceOrCreateBean(ConfigurableListableBeanFactory beanFactory, Be
138
144
}
139
145
}
140
146
else {
141
- // We will later generate a name for the nonexistent bean, but since NullAway
142
- // will reject leaving the beanName set to null, we set it to a placeholder.
147
+ // 2) We are creating a bean by-type, with a generated name.
148
+ // Since NullAway will reject leaving the beanName set to null,
149
+ // we set it to a placeholder that will be replaced later.
143
150
beanName = PSEUDO_BEAN_NAME_PLACEHOLDER ;
144
151
}
145
152
}
146
153
else {
147
154
Set <String > candidates = getExistingBeanNamesByType (beanFactory , handler , false );
148
155
if (candidates .contains (beanName )) {
149
- // We are overriding an existing bean by-name.
156
+ // 3) We are overriding an existing bean by-name.
150
157
existingBeanDefinition = beanFactory .getBeanDefinition (beanName );
151
158
}
152
159
else if (requireExistingBean ) {
@@ -156,6 +163,7 @@ else if (requireExistingBean) {
156
163
.formatted (beanName , handler .getBeanType (),
157
164
field .getDeclaringClass ().getSimpleName (), field .getName ()));
158
165
}
166
+ // 4) We are creating a bean by-name with the provided beanName.
159
167
}
160
168
161
169
if (existingBeanDefinition != null ) {
@@ -214,11 +222,14 @@ else if (Boolean.getBoolean(AbstractAotProcessor.AOT_PROCESSING)) {
214
222
215
223
/**
216
224
* Check that a bean with the specified {@link BeanOverrideHandler#getBeanName() name}
217
- * and {@link BeanOverrideHandler#getBeanType() type} is registered.
218
- * <p>If so, put the {@link BeanOverrideHandler} in the early tracking map.
219
- * <p>The map will later be checked to see if a given bean should be wrapped
220
- * upon creation, during the {@link WrapEarlyBeanPostProcessor#getEarlyBeanReference}
221
- * phase.
225
+ * or {@link BeanOverrideHandler#getBeanType() type} has already been registered
226
+ * in the {@code BeanFactory}.
227
+ * <p>If so, register the {@link BeanOverrideHandler} and the corresponding bean
228
+ * name in the {@link BeanOverrideRegistry}.
229
+ * <p>The registry will later be checked to see if a given bean should be wrapped
230
+ * upon creation, during the early bean post-processing phase.
231
+ * @see BeanOverrideRegistry#registerBeanOverrideHandler(BeanOverrideHandler, String)
232
+ * @see WrapEarlyBeanPostProcessor#getEarlyBeanReference(Object, String)
222
233
*/
223
234
private void wrapBean (ConfigurableListableBeanFactory beanFactory , BeanOverrideHandler handler ) {
224
235
String beanName = handler .getBeanName ();
@@ -393,7 +404,7 @@ private static String determinePrimaryCandidate(
393
404
* respectively.
394
405
* <p>The returned bean definition should <strong>not</strong> be used to create
395
406
* a bean instance but rather only for the purpose of having suitable bean
396
- * definition metadata available in the {@link BeanFactory} — for example,
407
+ * definition metadata available in the {@code BeanFactory} — for example,
397
408
* for autowiring candidate resolution.
398
409
*/
399
410
private static RootBeanDefinition createPseudoBeanDefinition (BeanOverrideHandler handler ) {
0 commit comments