From 56dbe06d9b885951922fcfe98aa514b355e031c5 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 27 Nov 2020 17:00:16 +0100 Subject: [PATCH] Register @Bean definitions as dependent on containing configuration class Closes gh-26167 --- .../beans/factory/support/ConstructorResolver.java | 1 + .../annotation/ConfigurationClassPostProcessorTests.java | 9 ++++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java index f49690677a..a690473528 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java @@ -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; } diff --git a/spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorTests.java b/spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorTests.java index caf1063f1a..a25982a268 100644 --- a/spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorTests.java +++ b/spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorTests.java @@ -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 { 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 { 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