Why Spring Boot Starts Slowly: A Detailed Performance Analysis and Profiling Guide
This article investigates the reasons behind Spring Boot's slow startup by examining the macro initialization flow, pinpointing time‑consuming stages such as environment preparation and bean refresh, and providing profiling code examples to help developers understand and optimize the boot process.
In recent experiments with several open‑source frameworks, the author noticed that Spring Boot’s startup time is noticeably longer than expected, prompting a deep dive into the boot sequence to identify the bottlenecks.
1. Overview (前言) – The article starts with a simple MVC project to illustrate the overall time line and highlights the two most time‑consuming methods: prepareEnvironment and refreshContext .
2. Macro Route (宏观路线) – The high‑level initialization steps are listed, showing how Spring Boot prepares the environment, creates the application context, registers listeners, and finally refreshes the context.
3. Detailed Breakdown (细节)
3.1 prepareEnvironment – Loads configuration, sets profiles, and prepares the environment. The code snippet below shows the core logic:
public void run(String... args) {
// 3. Trigger start event
listeners.starting();
// 4. Prepare environment (load config, set profile, env vars)
ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
// 7. Create application context (Web or regular)
context = createApplicationContext();
// 8. Prepare context (set env, register initializers, load sources)
prepareContext(context, environment, listeners, applicationArguments, printedBanner);
// 9. Refresh context
refreshContext(context);
// 13. Trigger context refreshed event
listeners.started(context);
// 14. Run all registered runners
callRunners(context, applicationArguments);
// 15. Trigger application running event
listeners.running(context);
}3.2 refreshContext – Handles bean loading and initialization, which is the main source of delay. Key methods such as doGetBean and populateBean are invoked here.
3.3 Five Typical Slow Scenarios
Multiple container creation (e.g., SpringApplication.run() and Cloud Bootstrap each load the context separately).
Configuration listeners loading external resources (e.g., ConfigFileApplicationListener , BootstrapApplicationListener ).
Repeated refresh() calls causing many bean reloads.
Large numbers of beans leading to extensive post‑processor execution.
Third‑party client creation (e.g., Redis, MySQL) that opens connections and pools.
4. Profiling Code – The author provides a custom StopWatchExpand utility to record timestamps for each step, calculate intervals, percentages, and print a formatted table. The essential parts are shown below:
public static String start(String processLine, String node) {
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
String callerClass = stackTrace[2].getClassName().substring(
stackTrace[2].getClassName().lastIndexOf(".") + 1);
String callerMethod = stackTrace[2].getMethodName();
long currentTimeMillis = System.currentTimeMillis();
taskEntries.add(new TaskEntry(processLine, node, callerClass, callerMethod, currentTimeMillis));
return String.format("[ 流程: %s | 节点: %s | 调用类: %s | 调用方法: %s ] 监测到达时间: %s",
processLine, node, callerClass, callerMethod, formatTimestamp(currentTimeMillis));
}
public static void stop() {
logStatistics();
taskEntries.clear();
}
private static void logStatistics() {
if (taskEntries.isEmpty()) return;
// calculate total duration, format header and rows, then print
// (implementation omitted for brevity)
}
private static String formatTimestamp(long timestamp) {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(timestamp);
}
// Helper methods padString, padChineseNode, etc., omitted for brevity5. Summary – The profiling shows that client‑side connections (e.g., Redis, MySQL) and configuration loading are the biggest time sinks, followed by repeated bean refreshes. The author suggests checking newer Spring Boot versions and alternative lightweight frameworks for improvements.
Overall, the article provides a practical, code‑driven guide to understand and reduce Spring Boot startup latency.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.