Spring modules, Boot starters và auto-configuration
Spring module cung cấp API/implementation; starter gom dependencies; dependency management căn version; auto-configuration tạo bean có điều kiện. Bốn khái niệm liên quan nhưng không đồng nghĩa.
1. Bức tranh tổng thể
Spring Framework modules
└─ Core, Beans, Context, AOP, TX, MVC, WebFlux...
Spring Boot starter
└─ dependency descriptor kéo nhóm libraries phù hợp
Spring Boot BOM/dependency management
└─ căn versions đã được kiểm thử cùng nhau
Spring Boot auto-configuration
└─ tạo beans nếu classpath/property/bean conditions match
spring-boot-starter-data-jpa kéo Spring Data JPA, Hibernate và JDBC dependencies; auto-configuration thấy classes cùng datasource properties rồi tạo infrastructure beans nếu application chưa tự định nghĩa.2. Spring Framework modules
| Nhóm | Modules/capabilities | Vai trò |
|---|---|---|
| Core container | spring-core, beans, context, SpEL | IoC, resources, events và bean context. |
| AOP | spring-aop, aspects | Proxy, advice và AspectJ integration. |
| Data | JDBC, TX, ORM, JMS | Data access và transaction abstraction. |
| Web | Web, MVC, WebFlux, WebSocket | HTTP, Servlet MVC và reactive web. |
| Testing | spring-test | Context integration, MockMvc và test utilities. |
Không cần thuộc mọi artifact; cần hiểu capability thuộc layer nào và dependency có mặt do dùng trực tiếp, transitive dependency hay auto-configuration.
3. Starter là gì?
Starter chủ yếu là dependency descriptor có tên và convention rõ để kéo dependency set thông dụng. Nó không phải runtime container mới và thường có rất ít application logic.
| Starter | Dùng cho | Điểm cần nhớ |
|---|---|---|
starter-web | Servlet/MVC REST | MVC, JSON, embedded Tomcat mặc định. |
starter-webflux | Reactive HTTP | Không biến blocking JPA/JDBC thành non-blocking. |
starter-data-jpa | JPA/Hibernate | Cần driver/datasource; vẫn phải xử lý transaction và N+1. |
starter-security | Security foundation | Dependency không thay security design. |
starter-validation | Bean Validation | Không thay DB hoặc business invariant. |
starter-actuator | Health/metrics/management | Giới hạn exposure và access. |
starter-test | JUnit, Spring Test và mocks | Dùng test scope; không phải test nào cũng cần full context. |
4. Starter khác BOM và plugin
Starter quyết định artifact nào được kéo. BOM hoặc dependency management quyết định version nào. Parent và build plugin còn cung cấp build defaults hoặc executable packaging; import BOM không tự áp toàn bộ plugin configuration.
Override riêng một library version có thể cần để vá CVE, nhưng phá tested set và có thể gây NoSuchMethodError. Phải kiểm tra dependency tree, release notes và integration tests.
5. Starter kích hoạt auto-configuration
- Starter đưa libraries và auto-config module vào classpath.
- Boot import auto-configuration classes từ metadata.
- Conditions theo class, bean, property và web type được đánh giá.
- Default beans được tạo khi đủ điều kiện.
- User bean hoặc property có thể làm auto-configuration back off.
“Thêm starter là mọi thứ tự chạy” sai khi thiếu property, driver, bean hoặc condition không match. Dùng Condition Evaluation Report để biết chính xác lý do.
6. Exclude và thay implementation
Có thể exclude Tomcat rồi thêm Jetty, hoặc thay logging stack. Nhưng exclude transitive dependency tùy tiện có thể gây class-loading hoặc auto-configuration failure. Xác minh bằng Maven dependency:tree, Gradle dependency insight và smoke tests.
7. Custom starter
acme-feature-spring-boot-autoconfigure
├─ @AutoConfiguration
├─ @ConfigurationProperties
├─ conditional beans
└─ AutoConfiguration.imports
acme-feature-spring-boot-starter
└─ dependencies tới autoconfigure + required libraries
- Dùng prefix property riêng, typed configuration và validation.
@ConditionalOnMissingBeanđể consumer override.- Không scan package consumer hoặc tạo network client ngoài ý muốn.
- Dùng
ApplicationContextRunnertest condition match và back-off. - Boot hiện đại dùng
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports.
8. Web và WebFlux cùng classpath
Thêm cả hai starter không có nghĩa chạy đồng thời hai server models. Boot suy luận application type; MVC thường được chọn khi cả hai hiện diện, trong khi WebClient vẫn dùng được trong MVC. Chọn dependencies có chủ đích và kiểm tra condition report.
9. Dependency troubleshooting
- Xem dependency tree hoặc dependency insight.
- Xác định dependency trực tiếp, transitive và version mediation.
- Kiểm tra Boot compatibility matrix và release notes.
- Đọc root
ClassNotFoundException,NoSuchMethodErrorhoặc condition report. - Loại override/exclusion không cần thiết và chạy lại integration/smoke tests.
10. Checklist phỏng vấn
- Phân biệt module, starter, BOM và auto-configuration.
- Kể starters đã dùng cùng use case cụ thể.
- Giải thích conditions và back-off.
- Biết xem dependency tree và xử lý conflict.
- Giải thích exclude server/logging và trade-off.
- Mô tả custom starter structure và tests.
- Biết metadata
AutoConfiguration.imports.