@@ -58,15 +58,14 @@ public static void invokeBeanFactoryPostProcessors(
5858 if (beanFactory instanceof BeanDefinitionRegistry ) {
5959 BeanDefinitionRegistry registry = (BeanDefinitionRegistry ) beanFactory ;
6060 List <BeanFactoryPostProcessor > regularPostProcessors = new LinkedList <BeanFactoryPostProcessor >();
61- List <BeanDefinitionRegistryPostProcessor > registryPostProcessors =
62- new LinkedList <BeanDefinitionRegistryPostProcessor >();
61+ List <BeanDefinitionRegistryPostProcessor > registryProcessors = new LinkedList <BeanDefinitionRegistryPostProcessor >();
6362
6463 for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors ) {
6564 if (postProcessor instanceof BeanDefinitionRegistryPostProcessor ) {
66- BeanDefinitionRegistryPostProcessor registryPostProcessor =
65+ BeanDefinitionRegistryPostProcessor registryProcessor =
6766 (BeanDefinitionRegistryPostProcessor ) postProcessor ;
68- registryPostProcessor .postProcessBeanDefinitionRegistry (registry );
69- registryPostProcessors .add (registryPostProcessor );
67+ registryProcessor .postProcessBeanDefinitionRegistry (registry );
68+ registryProcessors .add (registryProcessor );
7069 }
7170 else {
7271 regularPostProcessors .add (postProcessor );
@@ -77,33 +76,34 @@ public static void invokeBeanFactoryPostProcessors(
7776 // uninitialized to let the bean factory post-processors apply to them!
7877 // Separate between BeanDefinitionRegistryPostProcessors that implement
7978 // PriorityOrdered, Ordered, and the rest.
80- String [] postProcessorNames =
81- beanFactory .getBeanNamesForType (BeanDefinitionRegistryPostProcessor .class , true , false );
79+ List <BeanDefinitionRegistryPostProcessor > currentRegistryProcessors = new ArrayList <BeanDefinitionRegistryPostProcessor >();
8280
8381 // First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
84- List <BeanDefinitionRegistryPostProcessor > priorityOrderedPostProcessors = new ArrayList <BeanDefinitionRegistryPostProcessor >();
82+ String [] postProcessorNames =
83+ beanFactory .getBeanNamesForType (BeanDefinitionRegistryPostProcessor .class , true , false );
8584 for (String ppName : postProcessorNames ) {
8685 if (beanFactory .isTypeMatch (ppName , PriorityOrdered .class )) {
87- priorityOrderedPostProcessors .add (beanFactory .getBean (ppName , BeanDefinitionRegistryPostProcessor .class ));
86+ currentRegistryProcessors .add (beanFactory .getBean (ppName , BeanDefinitionRegistryPostProcessor .class ));
8887 processedBeans .add (ppName );
8988 }
9089 }
91- sortPostProcessors (beanFactory , priorityOrderedPostProcessors );
92- registryPostProcessors .addAll (priorityOrderedPostProcessors );
93- invokeBeanDefinitionRegistryPostProcessors (priorityOrderedPostProcessors , registry );
90+ sortPostProcessors (currentRegistryProcessors , beanFactory );
91+ registryProcessors .addAll (currentRegistryProcessors );
92+ invokeBeanDefinitionRegistryPostProcessors (currentRegistryProcessors , registry );
93+ currentRegistryProcessors .clear ();
9494
9595 // Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
9696 postProcessorNames = beanFactory .getBeanNamesForType (BeanDefinitionRegistryPostProcessor .class , true , false );
97- List <BeanDefinitionRegistryPostProcessor > orderedPostProcessors = new ArrayList <BeanDefinitionRegistryPostProcessor >();
9897 for (String ppName : postProcessorNames ) {
9998 if (!processedBeans .contains (ppName ) && beanFactory .isTypeMatch (ppName , Ordered .class )) {
100- orderedPostProcessors .add (beanFactory .getBean (ppName , BeanDefinitionRegistryPostProcessor .class ));
99+ currentRegistryProcessors .add (beanFactory .getBean (ppName , BeanDefinitionRegistryPostProcessor .class ));
101100 processedBeans .add (ppName );
102101 }
103102 }
104- sortPostProcessors (beanFactory , orderedPostProcessors );
105- registryPostProcessors .addAll (orderedPostProcessors );
106- invokeBeanDefinitionRegistryPostProcessors (orderedPostProcessors , registry );
103+ sortPostProcessors (currentRegistryProcessors , beanFactory );
104+ registryProcessors .addAll (currentRegistryProcessors );
105+ invokeBeanDefinitionRegistryPostProcessors (currentRegistryProcessors , registry );
106+ currentRegistryProcessors .clear ();
107107
108108 // Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
109109 boolean reiterate = true ;
@@ -112,17 +112,19 @@ public static void invokeBeanFactoryPostProcessors(
112112 postProcessorNames = beanFactory .getBeanNamesForType (BeanDefinitionRegistryPostProcessor .class , true , false );
113113 for (String ppName : postProcessorNames ) {
114114 if (!processedBeans .contains (ppName )) {
115- BeanDefinitionRegistryPostProcessor pp = beanFactory .getBean (ppName , BeanDefinitionRegistryPostProcessor .class );
116- registryPostProcessors .add (pp );
115+ currentRegistryProcessors .add (beanFactory .getBean (ppName , BeanDefinitionRegistryPostProcessor .class ));
117116 processedBeans .add (ppName );
118- pp .postProcessBeanDefinitionRegistry (registry );
119117 reiterate = true ;
120118 }
121119 }
120+ sortPostProcessors (currentRegistryProcessors , beanFactory );
121+ registryProcessors .addAll (currentRegistryProcessors );
122+ invokeBeanDefinitionRegistryPostProcessors (currentRegistryProcessors , registry );
123+ currentRegistryProcessors .clear ();
122124 }
123125
124126 // Now, invoke the postProcessBeanFactory callback of all processors handled so far.
125- invokeBeanFactoryPostProcessors (registryPostProcessors , beanFactory );
127+ invokeBeanFactoryPostProcessors (registryProcessors , beanFactory );
126128 invokeBeanFactoryPostProcessors (regularPostProcessors , beanFactory );
127129 }
128130
@@ -157,15 +159,15 @@ else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
157159 }
158160
159161 // First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
160- sortPostProcessors (beanFactory , priorityOrderedPostProcessors );
162+ sortPostProcessors (priorityOrderedPostProcessors , beanFactory );
161163 invokeBeanFactoryPostProcessors (priorityOrderedPostProcessors , beanFactory );
162164
163165 // Next, invoke the BeanFactoryPostProcessors that implement Ordered.
164166 List <BeanFactoryPostProcessor > orderedPostProcessors = new ArrayList <BeanFactoryPostProcessor >();
165167 for (String postProcessorName : orderedPostProcessorNames ) {
166168 orderedPostProcessors .add (beanFactory .getBean (postProcessorName , BeanFactoryPostProcessor .class ));
167169 }
168- sortPostProcessors (beanFactory , orderedPostProcessors );
170+ sortPostProcessors (orderedPostProcessors , beanFactory );
169171 invokeBeanFactoryPostProcessors (orderedPostProcessors , beanFactory );
170172
171173 // Finally, invoke all other BeanFactoryPostProcessors.
@@ -214,7 +216,7 @@ else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
214216 }
215217
216218 // First, register the BeanPostProcessors that implement PriorityOrdered.
217- sortPostProcessors (beanFactory , priorityOrderedPostProcessors );
219+ sortPostProcessors (priorityOrderedPostProcessors , beanFactory );
218220 registerBeanPostProcessors (beanFactory , priorityOrderedPostProcessors );
219221
220222 // Next, register the BeanPostProcessors that implement Ordered.
@@ -226,7 +228,7 @@ else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
226228 internalPostProcessors .add (pp );
227229 }
228230 }
229- sortPostProcessors (beanFactory , orderedPostProcessors );
231+ sortPostProcessors (orderedPostProcessors , beanFactory );
230232 registerBeanPostProcessors (beanFactory , orderedPostProcessors );
231233
232234 // Now, register all regular BeanPostProcessors.
@@ -241,15 +243,15 @@ else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
241243 registerBeanPostProcessors (beanFactory , nonOrderedPostProcessors );
242244
243245 // Finally, re-register all internal BeanPostProcessors.
244- sortPostProcessors (beanFactory , internalPostProcessors );
246+ sortPostProcessors (internalPostProcessors , beanFactory );
245247 registerBeanPostProcessors (beanFactory , internalPostProcessors );
246248
247249 // Re-register post-processor for detecting inner beans as ApplicationListeners,
248250 // moving it to the end of the processor chain (for picking up proxies etc).
249251 beanFactory .addBeanPostProcessor (new ApplicationListenerDetector (applicationContext ));
250252 }
251253
252- private static void sortPostProcessors (ConfigurableListableBeanFactory beanFactory , List <?> postProcessors ) {
254+ private static void sortPostProcessors (List <?> postProcessors , ConfigurableListableBeanFactory beanFactory ) {
253255 Comparator <Object > comparatorToUse = null ;
254256 if (beanFactory instanceof DefaultListableBeanFactory ) {
255257 comparatorToUse = ((DefaultListableBeanFactory ) beanFactory ).getDependencyComparator ();
0 commit comments