Can Spring’s Bean Scanning Be Parallelized? A Deep Dive into the Answer

This article investigates whether Spring’s class‑path bean scanning can be made concurrent, walks through source‑code verification, examines relevant GitHub issues and official responses, and finally offers practical tips for speeding up Spring Boot startup.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Can Spring’s Bean Scanning Be Parallelized? A Deep Dive into the Answer

Interview Question: Parallelizing Spring Bean Scanning

spring 在启动期间会做类扫描,以单例模式放入 ioc。但是 spring 只是一个个类进行处理,如果为了加速,我们取消 spring 自带的类扫描功能,用写代码的多线程方式并行进行处理,这种方案可行吗?为什么?

To verify the claim that Spring processes beans one by one, the author creates a simple bean Person and enables debug logging: logging.level.root=debug Running the application produces a log line like:

Identified candidate component class: xxxx.Person.class]

Searching the source for the class name leads to the method responsible for scanning:

org.springframework.context.annotation.ClassPathBeanDefinitionScanner#doScan

The implementation uses a simple for loop, confirming that bean registration is single‑threaded.

In theory, two unrelated beans (e.g., Person and Student) could be processed in parallel, as illustrated below:

如果为了加速,我们取消 spring 自带的类扫描功能,用写代码的多线程方式并行进行处理,这样可以吗?

The author first guessed the answer would be "no" because Spring 5.x processes beans sequentially, and a framework as widely used as Spring would have added parallel loading long ago if it were feasible.

Searching GitHub issues for the method name yields issue #28221, where a Spring contributor replies:

but not possible. 目前不可能异步的对 bean 进行后置处理。

Later, issue #13410 (opened in 2011) discusses parallel bean initialization. The official response indicates that even in Spring 6.0 the feature is not a priority; the focus is on Ahead‑of‑Time (AOT) compilation and improving startup time through other means.

Key take‑aways from the discussion:

Parallel bean initialization is technically possible but introduces complexity, bugs, and limited performance gains because the critical path often involves a few inter‑dependent beans.

Spring’s roadmap favours AOT compilation and startup tracing over parallel scanning.

Practical Tips to Speed Up Spring Boot Startup

Exclude unnecessary classpath entries (e.g., Hibernate Validator, Jackson) from Spring Boot web starters.

Use spring-context-indexer to speed up component scanning.

Avoid spring-boot-starter-actuator if not needed.

Upgrade to the latest Spring Boot (2.2+) and Spring Framework (5.2+) versions.

Set spring.config.location explicitly to avoid scanning default locations.

Disable JMX with spring.jmx.enabled=false when not required.

Enable lazy initialization ( spring.main.lazy-initialization=true).

Run the fat‑jar with an explicit classpath instead of the bundled one.

Start the JVM with -noverify or -XX:TieredStopAtLevel=1 to skip tiered compilation.

These recommendations stem from the issues and official guidance discussed above and can help reduce Spring Boot startup time without attempting to parallelize bean scanning.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

springAOT compilationStartup Performancebean-scanningGitHub IssuesParallelization
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.