Consistently sort BeanDefinitionRegistryPostProcessors
Issue: SPR-16043
This commit is contained in:
parent
53091c76bf
commit
ad4c8e7c0d
|
@ -59,15 +59,14 @@ class PostProcessorRegistrationDelegate {
|
||||||
if (beanFactory instanceof BeanDefinitionRegistry) {
|
if (beanFactory instanceof BeanDefinitionRegistry) {
|
||||||
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
|
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
|
||||||
List<BeanFactoryPostProcessor> regularPostProcessors = new LinkedList<>();
|
List<BeanFactoryPostProcessor> regularPostProcessors = new LinkedList<>();
|
||||||
List<BeanDefinitionRegistryPostProcessor> registryPostProcessors =
|
List<BeanDefinitionRegistryPostProcessor> registryProcessors = new LinkedList<>();
|
||||||
new LinkedList<>();
|
|
||||||
|
|
||||||
for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
|
for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
|
||||||
if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
|
if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
|
||||||
BeanDefinitionRegistryPostProcessor registryPostProcessor =
|
BeanDefinitionRegistryPostProcessor registryProcessor =
|
||||||
(BeanDefinitionRegistryPostProcessor) postProcessor;
|
(BeanDefinitionRegistryPostProcessor) postProcessor;
|
||||||
registryPostProcessor.postProcessBeanDefinitionRegistry(registry);
|
registryProcessor.postProcessBeanDefinitionRegistry(registry);
|
||||||
registryPostProcessors.add(registryPostProcessor);
|
registryProcessors.add(registryProcessor);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
regularPostProcessors.add(postProcessor);
|
regularPostProcessors.add(postProcessor);
|
||||||
|
@ -78,33 +77,34 @@ class PostProcessorRegistrationDelegate {
|
||||||
// uninitialized to let the bean factory post-processors apply to them!
|
// uninitialized to let the bean factory post-processors apply to them!
|
||||||
// Separate between BeanDefinitionRegistryPostProcessors that implement
|
// Separate between BeanDefinitionRegistryPostProcessors that implement
|
||||||
// PriorityOrdered, Ordered, and the rest.
|
// PriorityOrdered, Ordered, and the rest.
|
||||||
String[] postProcessorNames =
|
List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();
|
||||||
beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
|
|
||||||
|
|
||||||
// First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
|
// First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
|
||||||
List<BeanDefinitionRegistryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
|
String[] postProcessorNames =
|
||||||
|
beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
|
||||||
for (String ppName : postProcessorNames) {
|
for (String ppName : postProcessorNames) {
|
||||||
if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
|
if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
|
||||||
priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
|
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
|
||||||
processedBeans.add(ppName);
|
processedBeans.add(ppName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sortPostProcessors(beanFactory, priorityOrderedPostProcessors);
|
sortPostProcessors(currentRegistryProcessors, beanFactory);
|
||||||
registryPostProcessors.addAll(priorityOrderedPostProcessors);
|
registryProcessors.addAll(currentRegistryProcessors);
|
||||||
invokeBeanDefinitionRegistryPostProcessors(priorityOrderedPostProcessors, registry);
|
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
|
||||||
|
currentRegistryProcessors.clear();
|
||||||
|
|
||||||
// Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
|
// Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
|
||||||
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
|
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
|
||||||
List<BeanDefinitionRegistryPostProcessor> orderedPostProcessors = new ArrayList<>();
|
|
||||||
for (String ppName : postProcessorNames) {
|
for (String ppName : postProcessorNames) {
|
||||||
if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
|
if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
|
||||||
orderedPostProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
|
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
|
||||||
processedBeans.add(ppName);
|
processedBeans.add(ppName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sortPostProcessors(beanFactory, orderedPostProcessors);
|
sortPostProcessors(currentRegistryProcessors, beanFactory);
|
||||||
registryPostProcessors.addAll(orderedPostProcessors);
|
registryProcessors.addAll(currentRegistryProcessors);
|
||||||
invokeBeanDefinitionRegistryPostProcessors(orderedPostProcessors, registry);
|
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
|
||||||
|
currentRegistryProcessors.clear();
|
||||||
|
|
||||||
// Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
|
// Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
|
||||||
boolean reiterate = true;
|
boolean reiterate = true;
|
||||||
|
@ -113,17 +113,19 @@ class PostProcessorRegistrationDelegate {
|
||||||
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
|
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
|
||||||
for (String ppName : postProcessorNames) {
|
for (String ppName : postProcessorNames) {
|
||||||
if (!processedBeans.contains(ppName)) {
|
if (!processedBeans.contains(ppName)) {
|
||||||
BeanDefinitionRegistryPostProcessor pp = beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class);
|
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
|
||||||
registryPostProcessors.add(pp);
|
|
||||||
processedBeans.add(ppName);
|
processedBeans.add(ppName);
|
||||||
pp.postProcessBeanDefinitionRegistry(registry);
|
|
||||||
reiterate = true;
|
reiterate = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
sortPostProcessors(currentRegistryProcessors, beanFactory);
|
||||||
|
registryProcessors.addAll(currentRegistryProcessors);
|
||||||
|
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
|
||||||
|
currentRegistryProcessors.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now, invoke the postProcessBeanFactory callback of all processors handled so far.
|
// Now, invoke the postProcessBeanFactory callback of all processors handled so far.
|
||||||
invokeBeanFactoryPostProcessors(registryPostProcessors, beanFactory);
|
invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
|
||||||
invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
|
invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -158,7 +160,7 @@ class PostProcessorRegistrationDelegate {
|
||||||
}
|
}
|
||||||
|
|
||||||
// First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
|
// First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
|
||||||
sortPostProcessors(beanFactory, priorityOrderedPostProcessors);
|
sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
|
||||||
invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);
|
invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);
|
||||||
|
|
||||||
// Next, invoke the BeanFactoryPostProcessors that implement Ordered.
|
// Next, invoke the BeanFactoryPostProcessors that implement Ordered.
|
||||||
|
@ -166,7 +168,7 @@ class PostProcessorRegistrationDelegate {
|
||||||
for (String postProcessorName : orderedPostProcessorNames) {
|
for (String postProcessorName : orderedPostProcessorNames) {
|
||||||
orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
|
orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
|
||||||
}
|
}
|
||||||
sortPostProcessors(beanFactory, orderedPostProcessors);
|
sortPostProcessors(orderedPostProcessors, beanFactory);
|
||||||
invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);
|
invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);
|
||||||
|
|
||||||
// Finally, invoke all other BeanFactoryPostProcessors.
|
// Finally, invoke all other BeanFactoryPostProcessors.
|
||||||
|
@ -215,7 +217,7 @@ class PostProcessorRegistrationDelegate {
|
||||||
}
|
}
|
||||||
|
|
||||||
// First, register the BeanPostProcessors that implement PriorityOrdered.
|
// First, register the BeanPostProcessors that implement PriorityOrdered.
|
||||||
sortPostProcessors(beanFactory, priorityOrderedPostProcessors);
|
sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
|
||||||
registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);
|
registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);
|
||||||
|
|
||||||
// Next, register the BeanPostProcessors that implement Ordered.
|
// Next, register the BeanPostProcessors that implement Ordered.
|
||||||
|
@ -227,7 +229,7 @@ class PostProcessorRegistrationDelegate {
|
||||||
internalPostProcessors.add(pp);
|
internalPostProcessors.add(pp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sortPostProcessors(beanFactory, orderedPostProcessors);
|
sortPostProcessors(orderedPostProcessors, beanFactory);
|
||||||
registerBeanPostProcessors(beanFactory, orderedPostProcessors);
|
registerBeanPostProcessors(beanFactory, orderedPostProcessors);
|
||||||
|
|
||||||
// Now, register all regular BeanPostProcessors.
|
// Now, register all regular BeanPostProcessors.
|
||||||
|
@ -242,7 +244,7 @@ class PostProcessorRegistrationDelegate {
|
||||||
registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);
|
registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);
|
||||||
|
|
||||||
// Finally, re-register all internal BeanPostProcessors.
|
// Finally, re-register all internal BeanPostProcessors.
|
||||||
sortPostProcessors(beanFactory, internalPostProcessors);
|
sortPostProcessors(internalPostProcessors, beanFactory);
|
||||||
registerBeanPostProcessors(beanFactory, internalPostProcessors);
|
registerBeanPostProcessors(beanFactory, internalPostProcessors);
|
||||||
|
|
||||||
// Re-register post-processor for detecting inner beans as ApplicationListeners,
|
// Re-register post-processor for detecting inner beans as ApplicationListeners,
|
||||||
|
@ -250,7 +252,7 @@ class PostProcessorRegistrationDelegate {
|
||||||
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
|
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void sortPostProcessors(ConfigurableListableBeanFactory beanFactory, List<?> postProcessors) {
|
private static void sortPostProcessors(List<?> postProcessors, ConfigurableListableBeanFactory beanFactory) {
|
||||||
Comparator<Object> comparatorToUse = null;
|
Comparator<Object> comparatorToUse = null;
|
||||||
if (beanFactory instanceof DefaultListableBeanFactory) {
|
if (beanFactory instanceof DefaultListableBeanFactory) {
|
||||||
comparatorToUse = ((DefaultListableBeanFactory) beanFactory).getDependencyComparator();
|
comparatorToUse = ((DefaultListableBeanFactory) beanFactory).getDependencyComparator();
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2016 the original author or authors.
|
* Copyright 2002-2017 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -31,6 +31,7 @@ import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.ApplicationEvent;
|
import org.springframework.context.ApplicationEvent;
|
||||||
import org.springframework.context.ApplicationListener;
|
import org.springframework.context.ApplicationListener;
|
||||||
import org.springframework.context.event.ContextRefreshedEvent;
|
import org.springframework.context.event.ContextRefreshedEvent;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
import org.springframework.core.PriorityOrdered;
|
import org.springframework.core.PriorityOrdered;
|
||||||
import org.springframework.tests.sample.beans.TestBean;
|
import org.springframework.tests.sample.beans.TestBean;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
@ -101,26 +102,40 @@ public class BeanFactoryPostProcessorTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBeanDefinitionRegistryPostProcessor() throws Exception {
|
public void testBeanDefinitionRegistryPostProcessor() {
|
||||||
StaticApplicationContext ac = new StaticApplicationContext();
|
StaticApplicationContext ac = new StaticApplicationContext();
|
||||||
ac.registerSingleton("tb1", TestBean.class);
|
ac.registerSingleton("tb1", TestBean.class);
|
||||||
ac.registerSingleton("tb2", TestBean.class);
|
ac.registerSingleton("tb2", TestBean.class);
|
||||||
|
ac.addBeanFactoryPostProcessor(new PrioritizedBeanDefinitionRegistryPostProcessor());
|
||||||
TestBeanDefinitionRegistryPostProcessor bdrpp = new TestBeanDefinitionRegistryPostProcessor();
|
TestBeanDefinitionRegistryPostProcessor bdrpp = new TestBeanDefinitionRegistryPostProcessor();
|
||||||
ac.addBeanFactoryPostProcessor(bdrpp);
|
ac.addBeanFactoryPostProcessor(bdrpp);
|
||||||
assertFalse(bdrpp.wasCalled);
|
assertFalse(bdrpp.wasCalled);
|
||||||
ac.refresh();
|
ac.refresh();
|
||||||
assertTrue(bdrpp.wasCalled);
|
assertTrue(bdrpp.wasCalled);
|
||||||
assertTrue(ac.getBean(TestBeanFactoryPostProcessor.class).wasCalled);
|
assertTrue(ac.getBean("bfpp1", TestBeanFactoryPostProcessor.class).wasCalled);
|
||||||
|
assertTrue(ac.getBean("bfpp2", TestBeanFactoryPostProcessor.class).wasCalled);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBeanDefinitionRegistryPostProcessorRegisteringAnother() throws Exception {
|
public void testBeanDefinitionRegistryPostProcessorRegisteringAnother() {
|
||||||
StaticApplicationContext ac = new StaticApplicationContext();
|
StaticApplicationContext ac = new StaticApplicationContext();
|
||||||
ac.registerSingleton("tb1", TestBean.class);
|
ac.registerSingleton("tb1", TestBean.class);
|
||||||
ac.registerSingleton("tb2", TestBean.class);
|
ac.registerSingleton("tb2", TestBean.class);
|
||||||
ac.registerBeanDefinition("bdrpp2", new RootBeanDefinition(TestBeanDefinitionRegistryPostProcessor2.class));
|
ac.registerBeanDefinition("bdrpp2", new RootBeanDefinition(OuterBeanDefinitionRegistryPostProcessor.class));
|
||||||
ac.refresh();
|
ac.refresh();
|
||||||
assertTrue(ac.getBean(TestBeanFactoryPostProcessor.class).wasCalled);
|
assertTrue(ac.getBean("bfpp1", TestBeanFactoryPostProcessor.class).wasCalled);
|
||||||
|
assertTrue(ac.getBean("bfpp2", TestBeanFactoryPostProcessor.class).wasCalled);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPrioritizedBeanDefinitionRegistryPostProcessorRegisteringAnother() {
|
||||||
|
StaticApplicationContext ac = new StaticApplicationContext();
|
||||||
|
ac.registerSingleton("tb1", TestBean.class);
|
||||||
|
ac.registerSingleton("tb2", TestBean.class);
|
||||||
|
ac.registerBeanDefinition("bdrpp2", new RootBeanDefinition(PrioritizedOuterBeanDefinitionRegistryPostProcessor.class));
|
||||||
|
ac.refresh();
|
||||||
|
assertTrue(ac.getBean("bfpp1", TestBeanFactoryPostProcessor.class).wasCalled);
|
||||||
|
assertTrue(ac.getBean("bfpp2", TestBeanFactoryPostProcessor.class).wasCalled);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -159,13 +174,32 @@ public class BeanFactoryPostProcessorTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static class PrioritizedBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor, Ordered {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return Ordered.HIGHEST_PRECEDENCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
|
||||||
|
registry.registerBeanDefinition("bfpp1", new RootBeanDefinition(TestBeanFactoryPostProcessor.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public static class TestBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
|
public static class TestBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
|
||||||
|
|
||||||
public boolean wasCalled;
|
public boolean wasCalled;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
|
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
|
||||||
registry.registerBeanDefinition("bfpp", new RootBeanDefinition(TestBeanFactoryPostProcessor.class));
|
assertTrue(registry.containsBeanDefinition("bfpp1"));
|
||||||
|
registry.registerBeanDefinition("bfpp2", new RootBeanDefinition(TestBeanFactoryPostProcessor.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -175,16 +209,22 @@ public class BeanFactoryPostProcessorTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static class TestBeanDefinitionRegistryPostProcessor2 implements BeanDefinitionRegistryPostProcessor, PriorityOrdered {
|
public static class OuterBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
|
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
|
||||||
registry.registerBeanDefinition("anotherpp", new RootBeanDefinition(TestBeanDefinitionRegistryPostProcessor.class));
|
registry.registerBeanDefinition("anotherpp", new RootBeanDefinition(TestBeanDefinitionRegistryPostProcessor.class));
|
||||||
|
registry.registerBeanDefinition("ppp", new RootBeanDefinition(PrioritizedBeanDefinitionRegistryPostProcessor.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
|
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static class PrioritizedOuterBeanDefinitionRegistryPostProcessor extends OuterBeanDefinitionRegistryPostProcessor
|
||||||
|
implements PriorityOrdered {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getOrder() {
|
public int getOrder() {
|
||||||
|
|
Loading…
Reference in New Issue