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
- Suy luận application type: servlet, reactive hoặc non-web.
- Chuẩn bị
Environmentvà Config Data. - Áp listeners và initializers phù hợp giai đoạn.
- Tạo
ApplicationContextvà load bean definitions. - Refresh context: chạy post-processors, tạo singleton và khởi động lifecycle components.
- Publish startup/ready events rồi chạy
ApplicationRunnervàCommandLineRunner.
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 và @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.
| Condition | Câu hỏi nó trả lời |
|---|---|
@ConditionalOnClass | Technology/API có trên classpath không? |
@ConditionalOnMissingBean | User đã cung cấp implementation riêng chưa? |
@ConditionalOnProperty | Feature/configuration flag có bật không? |
| Web application condition | Context đ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.
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.