Part 03 · Spring Framework & Spring Boot · 3.1.06

Spring Boot startup và auto-configuration

Spring Boot không thay Spring Framework; nó điều phối bootstrap, dependency alignment, conditional configuration, embedded runtime và production tooling để tạo một application context có convention rõ.


SpringApplication lifecycle

  1. Suy luận application type: servlet, reactive hoặc non-web.
  2. Chuẩn bị Environment và Config Data.
  3. Áp listeners và initializers phù hợp giai đoạn.
  4. Tạo ApplicationContext và load bean definitions.
  5. Refresh context: chạy post-processors, tạo singleton và khởi động lifecycle components.
  6. Publish startup/ready events rồi chạy ApplicationRunnerCommandLineRunner.

Startup events có thứ tự; listener rất sớm không thể phụ thuộc bean vì context chưa tồn tại. Runner chạy sau context refresh nhưng trước khi startup được coi là hoàn tất, nên tác vụ chậm hoặc có thể fail phải có timeout và ownership rõ.

@SpringBootApplication

Annotation này kết hợp @SpringBootConfiguration (một dạng @Configuration), @EnableAutoConfiguration@ComponentScan. Vị trí package của application class xác định scan boundary mặc định, vì vậy đặt nó quá cao có thể kéo bean ngoài ý muốn; đặt quá thấp có thể bỏ sót components.

Auto-configuration và conditions

Auto-configuration classes được import theo metadata và conditions như class present, bean missing, property hoặc web type. Cấu hình của user thường làm auto-configuration back off. ConditionEvaluationReport cho biết condition matched hay không; exclusion nên là công cụ cuối sau khi đã hiểu property và bean condition.

ConditionCâu hỏi nó trả lời
@ConditionalOnClassTechnology/API có trên classpath không?
@ConditionalOnMissingBeanUser đã cung cấp implementation riêng chưa?
@ConditionalOnPropertyFeature/configuration flag có bật không?
Web application conditionContext đang chạy servlet hay reactive?

Starters và dependency management

Starter là dependency descriptor; BOM và build plugin align các versions đã được kiểm thử cùng nhau. Override một dependency riêng có thể gây binary incompatibility như NoSuchMethodError. Library starter nên tách autoconfigure module, dùng prefix properties rõ, conditions và configuration metadata.

Học sâu: xem 3.1.06A Spring modules, Boot starters, BOM và custom auto-configuration.

Config Data và precedence

Config có thể đến từ files, profile-specific documents, imports, environment variables, system properties và command-line arguments với precedence xác định. Dùng immutable @ConfigurationProperties cùng validation cho một nhóm config; không đọc Environment tùy tiện khắp business code. Secret nên đến từ secret manager hoặc mounted secret có policy, không hard-code trong config file.

Embedded server và packaging

Executable jar chứa nested dependencies và launcher; embedded server lifecycle gắn với application context. Layered jars hoặc container images giúp cache dependency layer. Graceful shutdown ngừng nhận request mới và chờ inflight trong timeout, nhưng ứng dụng vẫn phải chủ động dừng executors, schedulers và consumers.

Startup diagnostics

FailureAnalyzer làm thông báo dễ đọc hơn nhưng root cause chain vẫn là nguồn chính. Dùng startup endpoint hoặc JFR khi bean creation chậm; kiểm tra component scanning, network I/O trong initialization, database migrations và eager clients. Global lazy initialization chỉ chuyển failure từ startup sang request đầu tiên.

AOT và native image

AOT phân tích reflection, proxy và resources trước runtime để tạo hints cho native image. Dynamic classpath scanning hoặc reflection cần hints tương ứng. Native image có thể giảm startup và memory footprint nhưng tăng build complexity và giới hạn một số dynamic behavior; lựa chọn dựa trên deployment measurements, không theo trào lưu.

Failure-analysis flow: đọc root cause → kiểm tra condition report → xác nhận property source/precedence → xem bean graph và startup step → chỉ sau đó mới exclude auto-configuration hoặc bật lazy.
Nguồn tham khảo