From 69a95ce5c1e26ebc22b2fc4e1e1bec2c9e17c130 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 5 Nov 2019 09:53:36 +0000 Subject: [PATCH] Allow dependsOn relationships created by post-processor to be overridden Previously, AbstractDependsOnBeanFactoryPostProcessor was unordered which meant that it was impossible to guarantee that another bean factory post-processor would run after it. This prevented overriding of the dependsOn relationships that is creates. This commit updates AbstractDependsOnBeanFactoryPostProcessor to give it a default order of zero. This will allow additional bean factory post-processors to be configured with a lower precedence order (values greater than 0) so that they run after any AbstractDependsOnBeanFactoryPostProcessor and can override the dependencies that it has configured. Fixes gh-18362 --- .../AbstractDependsOnBeanFactoryPostProcessor.java | 8 +++++++- .../AbstractDependsOnBeanFactoryPostProcessorTests.java | 5 +++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AbstractDependsOnBeanFactoryPostProcessor.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AbstractDependsOnBeanFactoryPostProcessor.java index b945eaf99a..821a2692fc 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AbstractDependsOnBeanFactoryPostProcessor.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AbstractDependsOnBeanFactoryPostProcessor.java @@ -30,6 +30,7 @@ import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import org.springframework.core.Ordered; import org.springframework.util.StringUtils; /** @@ -45,7 +46,7 @@ import org.springframework.util.StringUtils; * @since 1.3.0 * @see BeanDefinition#setDependsOn(String[]) */ -public abstract class AbstractDependsOnBeanFactoryPostProcessor implements BeanFactoryPostProcessor { +public abstract class AbstractDependsOnBeanFactoryPostProcessor implements BeanFactoryPostProcessor, Ordered { private final Class beanClass; @@ -114,6 +115,11 @@ public abstract class AbstractDependsOnBeanFactoryPostProcessor implements BeanF } } + @Override + public int getOrder() { + return 0; + } + private Set getBeanNames(ListableBeanFactory beanFactory) { Set names = getBeanNames(beanFactory, this.beanClass); if (this.factoryBeanClass != null) { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/AbstractDependsOnBeanFactoryPostProcessorTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/AbstractDependsOnBeanFactoryPostProcessorTests.java index f379fa97dc..9eb213a18b 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/AbstractDependsOnBeanFactoryPostProcessorTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/AbstractDependsOnBeanFactoryPostProcessorTests.java @@ -73,6 +73,11 @@ public class AbstractDependsOnBeanFactoryPostProcessorTests { } } + @Test + public void postProcessorHasADefaultOrderOfZero() { + assertThat(new FooDependsOnBarTypePostProcessor().getOrder()).isEqualTo(0); + } + private void assertThatFooDependsOnBar(AssertableApplicationContext context) { ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); assertThat(getBeanDefinition("foo", beanFactory).getDependsOn()).containsExactly("bar", "barFactoryBean");