Summary:
This pull request introduces the ability to auto-configure a bootstrapExecutor
bean for background bean initialization in the Spring Framework. It provides an @BootstrapExecutor
annotation to configure the properties of the ThreadPoolTaskExecutor
used for background initialization.
Related Issues: https://github.com/spring-projects/spring-boot/issues/39791 https://github.com/spring-projects/spring-framework/issues/13410#issuecomment-1967596598
Changes:
- Added @BootstrapExecutor
annotation in org.springframework.context.annotation
package to configure the bootstrap executor.
- Modified ConfigurationClassPostProcessor
to detect the presence of @BootstrapExecutor
annotation on configuration classes and register a ThreadPoolTaskExecutor
bean with the configured properties.
- Added BootstrapExecutorBeanDefinitionParser
to parse the <context:bootstrap-executor>
XML element and register the ThreadPoolTaskExecutor
bean. (Note: This is currently not functional and the logic is temporarily handled in ConfigurationClassPostProcessor
.)
- Updated ContextNamespaceHandler
to register the BootstrapExecutorBeanDefinitionParser
.
- Added test cases in ConfigurationClassPostProcessorTests
to verify the parallel bean initialization using the @BootstrapExecutor
annotation.
Usage:
To use the bootstrapExecutor
for background bean initialization:
1. Add the @BootstrapExecutor
annotation to one of your @Configuration
classes.
2. Configure the desired properties of the ThreadPoolTaskExecutor
using the annotation attributes.
3. Use @Bean(bootstrap = BACKGROUND)
on the beans that should be initialized in the background.
Example:
@BootstrapExecutor(threadNamePrefix = "bootstrap-", corePoolSize = 2)
@Configuration
public class AppConfig {
@Bean(bootstrap = BACKGROUND)
public SlowInitBean slowBean() {
return new SlowInitBean();
}
}
Known Issues:
- The BootstrapExecutorBeanDefinitionParser
is currently not functional due to a parsing issue. The logic is temporarily handled in the processConfigBeanDefinitions
method of ConfigurationClassPostProcessor
.
Next Steps:
- Investigate and resolve the issue with BootstrapExecutorBeanDefinitionParser
to properly parse the <context:bootstrap-executor>
XML element.
- Consider automatically configuring a bootstrapExecutor
based on the number of @Bean(bootstrap=BACKGROUND)
annotations if @BootstrapExecutor
is not found.
Comment From: pivotal-cla
@ballista01 Please sign the Contributor License Agreement!
Click here to manually synchronize the status of this Pull Request.
See the FAQ for frequently asked questions.
Comment From: pivotal-cla
@ballista01 Thank you for signing the Contributor License Agreement!
Comment From: ballista01
@wilkinsona Thanks for notifying me the Springboot upgrade progress in https://github.com/spring-projects/spring-boot/issues/39791#issuecomment-2022124796. I found that I could try to solve the issue https://github.com/spring-projects/spring-boot/issues/39791 in Spring Framework first. The @BootstrapExecutor() annotation is working, but we could add other improvements. Do you think that I'm on the right track?
Comment From: snicoll
The change you're referring to must happen on the Spring Boot side of things. Andy was telling you we can't really work on the feature until Spring Boot switches to Spring Framework 6.2. He also gave you the timeline.