Skip to content

Commit 03fe1f0

Browse files
committed
Improve documentation for BeanOverrideBeanFactoryPostProcessor
1 parent 0d72477 commit 03fe1f0

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

spring-test/src/main/java/org/springframework/test/context/bean/override/BeanOverrideBeanFactoryPostProcessor.java

+26-15
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
import org.springframework.aop.scope.ScopedProxyUtils;
2525
import org.springframework.beans.BeansException;
26-
import org.springframework.beans.factory.BeanFactory;
2726
import org.springframework.beans.factory.BeanFactoryUtils;
2827
import org.springframework.beans.factory.FactoryBean;
2928
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
@@ -44,7 +43,7 @@
4443

4544
/**
4645
* 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}
4847
* accordingly.
4948
*
5049
* <p>For each override, the bean factory is prepared according to the chosen
@@ -119,17 +118,24 @@ private void replaceOrCreateBean(ConfigurableListableBeanFactory beanFactory, Be
119118

120119
// NOTE: This method supports 3 distinct scenarios which must be accounted for.
121120
//
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
125131

126132
String beanName = handler.getBeanName();
127133
Field field = handler.getField();
128134
BeanDefinition existingBeanDefinition = null;
129135
if (beanName == null) {
130136
beanName = getBeanNameForType(beanFactory, handler, requireExistingBean);
131137
if (beanName != null) {
132-
// We are overriding an existing bean by-type.
138+
// 1) We are overriding an existing bean by-type.
133139
beanName = BeanFactoryUtils.transformedBeanName(beanName);
134140
// If we are overriding a manually registered singleton, we won't find
135141
// an existing bean definition.
@@ -138,15 +144,16 @@ private void replaceOrCreateBean(ConfigurableListableBeanFactory beanFactory, Be
138144
}
139145
}
140146
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.
143150
beanName = PSEUDO_BEAN_NAME_PLACEHOLDER;
144151
}
145152
}
146153
else {
147154
Set<String> candidates = getExistingBeanNamesByType(beanFactory, handler, false);
148155
if (candidates.contains(beanName)) {
149-
// We are overriding an existing bean by-name.
156+
// 3) We are overriding an existing bean by-name.
150157
existingBeanDefinition = beanFactory.getBeanDefinition(beanName);
151158
}
152159
else if (requireExistingBean) {
@@ -156,6 +163,7 @@ else if (requireExistingBean) {
156163
.formatted(beanName, handler.getBeanType(),
157164
field.getDeclaringClass().getSimpleName(), field.getName()));
158165
}
166+
// 4) We are creating a bean by-name with the provided beanName.
159167
}
160168

161169
if (existingBeanDefinition != null) {
@@ -214,11 +222,14 @@ else if (Boolean.getBoolean(AbstractAotProcessor.AOT_PROCESSING)) {
214222

215223
/**
216224
* 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)
222233
*/
223234
private void wrapBean(ConfigurableListableBeanFactory beanFactory, BeanOverrideHandler handler) {
224235
String beanName = handler.getBeanName();
@@ -393,7 +404,7 @@ private static String determinePrimaryCandidate(
393404
* respectively.
394405
* <p>The returned bean definition should <strong>not</strong> be used to create
395406
* a bean instance but rather only for the purpose of having suitable bean
396-
* definition metadata available in the {@link BeanFactory} &mdash; for example,
407+
* definition metadata available in the {@code BeanFactory} &mdash; for example,
397408
* for autowiring candidate resolution.
398409
*/
399410
private static RootBeanDefinition createPseudoBeanDefinition(BeanOverrideHandler handler) {

0 commit comments

Comments
 (0)