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
This commit is contained in:
@@ -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<String> getBeanNames(ListableBeanFactory beanFactory) {
|
||||
Set<String> names = getBeanNames(beanFactory, this.beanClass);
|
||||
if (this.factoryBeanClass != null) {
|
||||
|
||||
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user