Browse Source

Register @Bean definitions as dependent on containing configuration class

Closes gh-26167
pull/26173/head
Juergen Hoeller 4 years ago
parent
commit
56dbe06d9b
  1. 1
      spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java
  2. 9
      spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorTests.java

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

@ -410,6 +410,7 @@ class ConstructorResolver { @@ -410,6 +410,7 @@ class ConstructorResolver {
if (mbd.isSingleton() && this.beanFactory.containsSingleton(beanName)) {
throw new ImplicitlyAppearedSingletonException();
}
this.beanFactory.registerDependentBean(factoryBeanName, beanName);
factoryClass = factoryBean.getClass();
isStatic = false;
}

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

@ -20,7 +20,6 @@ import java.lang.annotation.ElementType; @@ -20,7 +20,6 @@ import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@ -107,7 +106,9 @@ public class ConfigurationClassPostProcessorTests { @@ -107,7 +106,9 @@ public class ConfigurationClassPostProcessorTests {
Foo foo = beanFactory.getBean("foo", Foo.class);
Bar bar = beanFactory.getBean("bar", Bar.class);
assertThat(bar.foo).isSameAs(foo);
assertThat(Arrays.asList(beanFactory.getDependentBeans("foo")).contains("bar")).isTrue();
assertThat(ObjectUtils.containsElement(beanFactory.getDependentBeans("foo"), "bar")).isTrue();
assertThat(ObjectUtils.containsElement(beanFactory.getDependentBeans("config"), "foo")).isTrue();
assertThat(ObjectUtils.containsElement(beanFactory.getDependentBeans("config"), "bar")).isTrue();
}
@Test
@ -119,7 +120,9 @@ public class ConfigurationClassPostProcessorTests { @@ -119,7 +120,9 @@ public class ConfigurationClassPostProcessorTests {
Foo foo = beanFactory.getBean("foo", Foo.class);
Bar bar = beanFactory.getBean("bar", Bar.class);
assertThat(bar.foo).isSameAs(foo);
assertThat(Arrays.asList(beanFactory.getDependentBeans("foo")).contains("bar")).isTrue();
assertThat(ObjectUtils.containsElement(beanFactory.getDependentBeans("foo"), "bar")).isTrue();
assertThat(ObjectUtils.containsElement(beanFactory.getDependentBeans("config"), "foo")).isTrue();
assertThat(ObjectUtils.containsElement(beanFactory.getDependentBeans("config"), "bar")).isTrue();
}
@Test

Loading…
Cancel
Save