Browse Source

Track bean dependencies for calls between @Bean methods within @Configuration classes

Issue: SPR-15069
pull/1279/head
Juergen Hoeller 9 years ago
parent
commit
a5c6658d2c
  1. 13
      spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java
  2. 2
      spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorTests.java

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

@ -356,10 +356,10 @@ class ConfigurationClassEnhancer {
return cglibMethodProxy.invokeSuper(enhancedConfigInstance, beanMethodArgs); return cglibMethodProxy.invokeSuper(enhancedConfigInstance, beanMethodArgs);
} }
else { else {
// The user (i.e. not the factory) is requesting this bean through a // The user (i.e. not the factory) is requesting this bean through a call to
// call to the bean method, direct or indirect. The bean may have already been // the bean method, direct or indirect. The bean may have already been marked
// marked as 'in creation' in certain autowiring scenarios; if so, temporarily // as 'in creation' in certain autowiring scenarios; if so, temporarily set
// set the in-creation status to false in order to avoid an exception. // the in-creation status to false in order to avoid an exception.
boolean alreadyInCreation = beanFactory.isCurrentlyInCreation(beanName); boolean alreadyInCreation = beanFactory.isCurrentlyInCreation(beanName);
try { try {
if (alreadyInCreation) { if (alreadyInCreation) {
@ -393,6 +393,11 @@ class ConfigurationClassEnhancer {
} }
throw new IllegalStateException(msg); throw new IllegalStateException(msg);
} }
Method currentlyInvoked = SimpleInstantiationStrategy.getCurrentlyInvokedFactoryMethod();
if (currentlyInvoked != null) {
String outerBeanName = BeanAnnotationHelper.determineBeanNameFor(currentlyInvoked);
beanFactory.registerDependentBean(beanName, outerBeanName);
}
return beanInstance; return beanInstance;
} }
finally { finally {

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

@ -20,6 +20,7 @@ import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import java.util.Arrays;
import java.util.List; import java.util.List;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
@ -94,6 +95,7 @@ public class ConfigurationClassPostProcessorTests {
Foo foo = beanFactory.getBean("foo", Foo.class); Foo foo = beanFactory.getBean("foo", Foo.class);
Bar bar = beanFactory.getBean("bar", Bar.class); Bar bar = beanFactory.getBean("bar", Bar.class);
assertSame(foo, bar.foo); assertSame(foo, bar.foo);
assertTrue(Arrays.asList(beanFactory.getDependentBeans("foo")).contains("bar"));
} }
@Test @Test

Loading…
Cancel
Save