Repository: spring-projects/spring-framework

Issue #13410: Parallel bean initialization during startup [SPR-8767]
Created: 2011-10-12T14:39:05Z
Tomasz Nurkiewicz opened SPR-8767 and commented
Spring should provide a way (possibly a BeanFactory with a different ConfigurableListableBeanFactory#preInstantiateSingletons implementation) to initialize singleton non-lazy beans on startup in parallel using a thread pool. This could significantly reduce startup (and maybe shutdown) time by creating and initializing independent beans concurrently.
The algorithm is pretty simple in principle. Whereas the normal bean factory creates beans in single thread in rather random order, this implementation should:

Find all bean definitions that don't have any unresolved dependencies.
Schedule creation of each bean found in 1. in a separate concurrent task to allow parallel creation
When any of the tasks scheduled in 2. is completed go to 1.

The algorithm stops when all beans are created.
Implementation notes:

circular dependencies might require extra care
bean factory should create a thread pool with configurable number of threads and shut it down after all beans are created


Affects: 3.1 RC1
Reference URL: http://forum.springsource.org/showthread.php?105896-Initialize-spring-beans-in-parallel-at-startup
Issue Links:

#14520 Parallelize Component Scanning to Improve Spring Startup Time ("is duplicated by")
#10033 Threads bottlenecking in DefaultSingletonBeanRegistry when using Wicket's @SpringBean annotation for injection
#14520 Parallelize Component Scanning to Improve Spring Startup Time
#20904 Initialization blocked by multi-threaded event publishing
#19487 Asynchronous initialization of beans during startup
#18305 Background initialization option for JPA EntityManagerFactory / Hibernate SessionFactory
#19398 Add a functional way to register a bean

80 votes, 77 watchers

Duplicate issue #23294: Asynchronous initialization of Spring beans
Created: 2019-07-16T07:30:10Z
As the most people's problems, we have come to the same confusion. Our project has about 1,000 beans, but the startup time has been close to 10 minutes. Every time we fix a bug and do an integrated deployment, it takes a long time to wait.  There are two Suggestions could be taken  into consideration.


On the basis of existing source code, the initialization of beans with init method or afterPropetiesSet  left to the thread pool, but the dependency safety must be ensured, such as A->B, but B is not initialized. We can give each Bean  a proxy, ensure B initialized  at first through the way of  intercepting the proxy's methods.


Static analysis of dependencies between beans  generates multiple graphs.



Calculate Strongly Connected Component(SCC) of each Directed acyclic graph (DAG)  reference the Tarjan algorithm
Each SCC subgraph as a specific node
Generate a parallel  execution schedule based on topological sort.

Issue Links:
#13410 Parallel bean initialization during startup [SPR-8767]
#19487 Asynchronous initialization of beans during startup [SPR-14920]

Closing comment (2023-12-23T13:14:05Z): Closing this as a duplicate of #13410 and #19487 for further purposes. We are going to reconsider our options there for 6.2 or otherwise for 7.0.