spring-projects / spring-projects/spring-framework
Adjust registration order of BeanDefinition declared by @Configuration
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 60.2k
- Forks
- 38.8k
- Avg merge
- 5d 2h
- Merged PRs (30d)
- 27
Description
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.
@Importand@EnableConfigurationProperties) - (static) InnerConfiguration
- @Bean method
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:
private void loadBeanDefinitionsForConfigurationClass(
ConfigurationClass configClass, TrackedConditionEvaluator trackedConditionEvaluator) {
if (trackedConditionEvaluator.shouldSkip(configClass)) {
String beanName = 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 this
for (BeanMethod beanMethod : configClass.getBeanMethods()) {
loadBeanDefinitionsForBeanMethod(beanMethod);
}
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading ConfigurationClassParser and ConfigurationClassBeanDefinitionReader, then reproduce the registration-order issue with the linked demo2.zip. Compare the observed order with the proposed Configuration, imported beans, inner configuration, and @Bean sequence; done means the ordering and conditional skipping behavior are implemented and covered by tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring
- Domain
- backend
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100