You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Adjust registration order of BeanDefinition declared by @Configuration #35008
While I tried to customize an AutoConfiguration, but @ConditionalOnBean did not working, then I tried to find the problem through examples, source code, and the Internet, and used different configuration methods, and finally found that it was because of the registration order of BeanDefinition.
In my opinion, their registration order should be like this:
Configuration
beans annotated on Configuration (e.g. @Import and @EnableConfigurationProperties)
Then about ConfigurationClassParser, the (static) InnerConfiguration should be managed by the fields in ConfigurationClass of Configuration, and if the Conditional of Configuration is not matched, then InnerConfiguration should also be skipped, ConfigurationClassBeanDefinitionReader will be like:
privatevoidloadBeanDefinitionsForConfigurationClass(
ConfigurationClassconfigClass, TrackedConditionEvaluatortrackedConditionEvaluator) {
if (trackedConditionEvaluator.shouldSkip(configClass)) {
StringbeanName = configClass.getBeanName();
if (StringUtils.hasLength(beanName) && this.registry.containsBeanDefinition(beanName)) {
this.registry.removeBeanDefinition(beanName);
}
this.importRegistry.removeImportingClass(configClass.getMetadata().getClassName());
return;
}
if (configClass.isImported()) {
registerBeanDefinitionForImportedConfigurationClass(configClass);
}
loadBeanDefinitionsFromImportedResources(configClass.getImportedResources());
loadBeanDefinitionsFromRegistrars(configClass.getImportBeanDefinitionRegistrars());
loadBeanDefinitionsFromInnerConfigurationClass(configClass.getInnerConfigurationClass()); // something like thisfor (BeanMethodbeanMethod : configClass.getBeanMethods()) {
loadBeanDefinitionsForBeanMethod(beanMethod);
}
}
Let's take a step back.
Instead of describing the implementation changes you want, can you explain what are you trying to achieve and why it doesn't work?
Let's take a step back. Instead of describing the implementation changes you want, can you explain what are you trying to achieve and why it doesn't work?
Let me simplify it first, please check the code below:
parse the ConfigurationClass instance of FooServiceAutoConfiguration through ConfigurationClassParser, and registered BeanDefinition
load BeanDefinition by ConfigurationClassBeanDefinitionReader
2.1 check if FooServiceAutoConfiguration shouldSkip
2.2 registerBeanDefinitionForImportedConfigurationClass: annotation @Import
2.3 loadBeanDefinitionsForBeanMethod: annotation @Bean
2.4 loadBeanDefinitionsFromImportedResources
2.5 loadBeanDefinitionsFromRegistrars(renamed to loadBeanDefinitionsFromImportBeanDefinitionRegistrars in 7.x): annotation @EnableConfigurationProperties will be handled here
But, in step 2.3 loadBeanDefinitionsForBeanMethod, while check the conditional of @ConditionalOnBean(FooProperties.class), it found that there is no bean(or BeanDefinitation) for type FooProperties, because the BeanDefinition of type FooProperties load in step 2.5 loadBeanDefinitionsFromRegistrars.
I knew that if I remove @ConditionalOnBean(...), then it's will work well, but I still think it is a little strange that it cannot create beans when the condition exists. Would you or someone in spring team be willing to tell me the reason for this design?
changed the title [-]Adjus registration order of BeanDefinition declared by @Configuration[/-][+]Adjust registration order of BeanDefinition declared by @Configuration[/+]on Jan 9, 2026
Thanks for raising this, and sorry it has gone so long without a decision.
We're reviewing older issues to keep the tracker focused on work we can realistically plan. This request hasn't drawn much interest from the community and doesn't fit the current priorities, so we're declining it.
If it still matters to you on a current Spring Framework version, please add a comment with your use case and we can reconsider. A reproducer or concrete examples help a lot.
Activity
bclozel commentedon Jun 7, 2025
Let's take a step back.
Instead of describing the implementation changes you want, can you explain what are you trying to achieve and why it doesn't work?
xinbimingjingtai commentedon Jun 8, 2025
Let me simplify it first, please check the code below:
Its execution order is as follows:
ConfigurationClassinstance ofFooServiceAutoConfigurationthroughConfigurationClassParser, and registeredBeanDefinitionBeanDefinitionbyConfigurationClassBeanDefinitionReader2.1 check if
FooServiceAutoConfigurationshouldSkip2.2 registerBeanDefinitionForImportedConfigurationClass: annotation
@Import2.3 loadBeanDefinitionsForBeanMethod: annotation
@Bean2.4 loadBeanDefinitionsFromImportedResources
2.5 loadBeanDefinitionsFromRegistrars(renamed to
loadBeanDefinitionsFromImportBeanDefinitionRegistrarsin 7.x): annotation@EnableConfigurationPropertieswill be handled hereBut, in step
2.3 loadBeanDefinitionsForBeanMethod, while check the conditional of@ConditionalOnBean(FooProperties.class), it found that there is no bean(or BeanDefinitation) for typeFooProperties, because the BeanDefinition of typeFooPropertiesload in step2.5 loadBeanDefinitionsFromRegistrars.I knew that if I remove
@ConditionalOnBean(...), then it's will work well, but I still think it is a little strange that it cannot create beans when the condition exists. Would you or someone in spring team be willing to tell me the reason for this design?[-]Adjus registration order of BeanDefinition declared by @Configuration[/-][+]Adjust registration order of BeanDefinition declared by @Configuration[/+]bclozel commentedon Oct 23, 2026
Thanks for raising this, and sorry it has gone so long without a decision.
We're reviewing older issues to keep the tracker focused on work we can realistically plan. This request hasn't drawn much interest from the community and doesn't fit the current priorities, so we're declining it.
If it still matters to you on a current Spring Framework version, please add a comment with your use case and we can reconsider. A reproducer or concrete examples help a lot.