Skip to content

Commit 81c1b60

Browse files
committed
Register @bean definitions as dependent on containing configuration class
Closes gh-26167
1 parent 3703be5 commit 81c1b60

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666

6767
/**
6868
* Delegate for resolving constructors and factory methods.
69+
*
6970
* <p>Performs constructor resolution through argument matching.
7071
*
7172
* @author Juergen Hoeller
@@ -84,7 +85,7 @@ class ConstructorResolver {
8485
private static final Object[] EMPTY_ARGS = new Object[0];
8586

8687
/**
87-
* Marker for autowired arguments in a cached argument array, to be later replaced
88+
* Marker for autowired arguments in a cached argument array, to be replaced
8889
* by a {@linkplain #resolveAutowiredArgument resolved autowired argument}.
8990
*/
9091
private static final Object autowiredArgumentMarker = new Object();
@@ -148,7 +149,7 @@ public BeanWrapper autowireConstructor(String beanName, RootBeanDefinition mbd,
148149
}
149150
}
150151
if (argsToResolve != null) {
151-
argsToUse = resolvePreparedArguments(beanName, mbd, bw, constructorToUse, argsToResolve, true);
152+
argsToUse = resolvePreparedArguments(beanName, mbd, bw, constructorToUse, argsToResolve);
152153
}
153154
}
154155

@@ -409,6 +410,7 @@ public BeanWrapper instantiateUsingFactoryMethod(
409410
if (mbd.isSingleton() && this.beanFactory.containsSingleton(beanName)) {
410411
throw new ImplicitlyAppearedSingletonException();
411412
}
413+
this.beanFactory.registerDependentBean(factoryBeanName, beanName);
412414
factoryClass = factoryBean.getClass();
413415
isStatic = false;
414416
}
@@ -443,7 +445,7 @@ public BeanWrapper instantiateUsingFactoryMethod(
443445
}
444446
}
445447
if (argsToResolve != null) {
446-
argsToUse = resolvePreparedArguments(beanName, mbd, bw, factoryMethodToUse, argsToResolve, true);
448+
argsToUse = resolvePreparedArguments(beanName, mbd, bw, factoryMethodToUse, argsToResolve);
447449
}
448450
}
449451

@@ -815,7 +817,7 @@ private ArgumentsHolder createArgumentArray(
815817
* Resolve the prepared arguments stored in the given bean definition.
816818
*/
817819
private Object[] resolvePreparedArguments(String beanName, RootBeanDefinition mbd, BeanWrapper bw,
818-
Executable executable, Object[] argsToResolve, boolean fallback) {
820+
Executable executable, Object[] argsToResolve) {
819821

820822
TypeConverter customConverter = this.beanFactory.getCustomTypeConverter();
821823
TypeConverter converter = (customConverter != null ? customConverter : bw);
@@ -828,7 +830,7 @@ private Object[] resolvePreparedArguments(String beanName, RootBeanDefinition mb
828830
Object argValue = argsToResolve[argIndex];
829831
MethodParameter methodParam = MethodParameter.forExecutable(executable, argIndex);
830832
if (argValue == autowiredArgumentMarker) {
831-
argValue = resolveAutowiredArgument(methodParam, beanName, null, converter, fallback);
833+
argValue = resolveAutowiredArgument(methodParam, beanName, null, converter, true);
832834
}
833835
else if (argValue instanceof BeanMetadataElement) {
834836
argValue = valueResolver.resolveValueIfNecessary("constructor argument", argValue);

spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorTests.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,7 +20,6 @@
2020
import java.lang.annotation.Retention;
2121
import java.lang.annotation.RetentionPolicy;
2222
import java.lang.annotation.Target;
23-
import java.util.Arrays;
2423
import java.util.List;
2524
import java.util.Map;
2625

@@ -105,7 +104,9 @@ public void enhancementIsPresentBecauseSingletonSemanticsAreRespected() {
105104
Foo foo = beanFactory.getBean("foo", Foo.class);
106105
Bar bar = beanFactory.getBean("bar", Bar.class);
107106
assertThat(bar.foo).isSameAs(foo);
108-
assertThat(Arrays.asList(beanFactory.getDependentBeans("foo")).contains("bar")).isTrue();
107+
assertThat(ObjectUtils.containsElement(beanFactory.getDependentBeans("foo"), "bar")).isTrue();
108+
assertThat(ObjectUtils.containsElement(beanFactory.getDependentBeans("config"), "foo")).isTrue();
109+
assertThat(ObjectUtils.containsElement(beanFactory.getDependentBeans("config"), "bar")).isTrue();
109110
}
110111

111112
@Test
@@ -117,7 +118,9 @@ public void enhancementIsPresentBecauseSingletonSemanticsAreRespectedUsingAsm()
117118
Foo foo = beanFactory.getBean("foo", Foo.class);
118119
Bar bar = beanFactory.getBean("bar", Bar.class);
119120
assertThat(bar.foo).isSameAs(foo);
120-
assertThat(Arrays.asList(beanFactory.getDependentBeans("foo")).contains("bar")).isTrue();
121+
assertThat(ObjectUtils.containsElement(beanFactory.getDependentBeans("foo"), "bar")).isTrue();
122+
assertThat(ObjectUtils.containsElement(beanFactory.getDependentBeans("config"), "foo")).isTrue();
123+
assertThat(ObjectUtils.containsElement(beanFactory.getDependentBeans("config"), "bar")).isTrue();
121124
}
122125

123126
@Test

0 commit comments

Comments
 (0)