Skip to content

Commit a5c6658

Browse files
committed
Track bean dependencies for calls between @bean methods within @configuration classes
Issue: SPR-15069
1 parent edc62be commit a5c6658

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -356,10 +356,10 @@ public Object intercept(Object enhancedConfigInstance, Method beanMethod, Object
356356
return cglibMethodProxy.invokeSuper(enhancedConfigInstance, beanMethodArgs);
357357
}
358358
else {
359-
// The user (i.e. not the factory) is requesting this bean through a
360-
// call to the bean method, direct or indirect. The bean may have already been
361-
// marked as 'in creation' in certain autowiring scenarios; if so, temporarily
362-
// set the in-creation status to false in order to avoid an exception.
359+
// The user (i.e. not the factory) is requesting this bean through a call to
360+
// the bean method, direct or indirect. The bean may have already been marked
361+
// as 'in creation' in certain autowiring scenarios; if so, temporarily set
362+
// the in-creation status to false in order to avoid an exception.
363363
boolean alreadyInCreation = beanFactory.isCurrentlyInCreation(beanName);
364364
try {
365365
if (alreadyInCreation) {
@@ -393,6 +393,11 @@ public Object intercept(Object enhancedConfigInstance, Method beanMethod, Object
393393
}
394394
throw new IllegalStateException(msg);
395395
}
396+
Method currentlyInvoked = SimpleInstantiationStrategy.getCurrentlyInvokedFactoryMethod();
397+
if (currentlyInvoked != null) {
398+
String outerBeanName = BeanAnnotationHelper.determineBeanNameFor(currentlyInvoked);
399+
beanFactory.registerDependentBean(beanName, outerBeanName);
400+
}
396401
return beanInstance;
397402
}
398403
finally {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.lang.annotation.Retention;
2121
import java.lang.annotation.RetentionPolicy;
2222
import java.lang.annotation.Target;
23+
import java.util.Arrays;
2324
import java.util.List;
2425
import javax.annotation.PostConstruct;
2526

@@ -94,6 +95,7 @@ public void enhancementIsPresentBecauseSingletonSemanticsAreRespected() {
9495
Foo foo = beanFactory.getBean("foo", Foo.class);
9596
Bar bar = beanFactory.getBean("bar", Bar.class);
9697
assertSame(foo, bar.foo);
98+
assertTrue(Arrays.asList(beanFactory.getDependentBeans("foo")).contains("bar"));
9799
}
98100

99101
@Test

0 commit comments

Comments
 (0)