Bài 14 / 41~5 phútContainer InternalsMiễn phí… lượt xem
Container Internals — tổng kết
Tóm tắt module Container internals: data structure container, refresh() 12 bước, BFPP vs BPP, PropertySource ordering, bootstrap. Cheat sheet + self-assessment.
Bạn vừa mổ xong bên trong IoC container. Đây là bản đồ gói gọn để tra cứu nhanh.
Cheat sheet
| Khái niệm | Một câu cốt lõi | Bài |
|---|---|---|
BeanFactory | Hợp đồng tối thiểu: cho định danh → trả bean; mặc định lazy | 01 |
DefaultListableBeanFactory | Trái tim chứa bean: beanDefinitionMap (metadata) + singletonObjects (instance) | 01 |
| Singleton scope | Một entry cached trong singletonObjects của một container — KHÔNG phải static pattern | 01 |
ApplicationContext | Bọc (compose) một DefaultListableBeanFactory + 4 capability; tạo eager singleton (fail-fast) | 01 |
refresh() | 12 bước, 3 giai đoạn: load definition → BFPP/BPP register → instantiate + publish event | 02 |
BeanDefinition | Metadata bean (class, scope, dependencies) trong beanDefinitionMap, chưa phải instance | 03 |
| BFPP vs BPP | BFPP modify metadata (trước instantiate); BPP modify instance (quanh từng bean) | 03 |
Environment | Quản properties + profiles; PropertySource ưu tiên command line cao nhất | 04 |
| Relax binding | DB_URL env var → property db.url (chuẩn K8s/Docker) | 04 |
Resource | Trừu tượng hoá classpath:/file:/https:; classpath*: scan nhiều jar | 05 |
@SpringBootApplication | = @SpringBootConfiguration + @EnableAutoConfiguration + @ComponentScan | 05 |
Mental model gói gọn
Một container = một
DefaultListableBeanFactory= vài cái map.refresh()lấp đầy các map đó theo 12 bước. Bỏ chữ "magic", Spring container chỉ làMap<String, BeanDefinition>+Map<String, Object>cùng logic tra/tạo quanh chúng.
✅ Self-assessment
Bạn đã đạt module này nếu trả lời được:
- Giải thích được BeanFactory vs ApplicationContext và data structure thật (
singletonObjects,beanDefinitionMap) - Trace được
refresh()12 bước qua 3 giai đoạn biến config thành runtime container - So sánh được BeanFactoryPostProcessor (metadata) vs BeanPostProcessor (instance) và BeanDefinition
- Cấu hình được externalized config với Environment + PropertySource đúng thứ tự ưu tiên
- Chẩn đoán được lỗi container (bean không scan, sai thứ tự property) qua Resource + @SpringBootApplication
Liên hệ các module khác
- Nhập môn & IoC/DI — concept IoC/DI mà container này thực thi.
- Bean Lifecycle & Scopes — bước 11 của
refresh()(createBean) mở rộng thành vòng đời từng bean. - Spring Boot Foundations —
@EnableAutoConfigurationđào sâu ở đây.
⁂
Module tiếp theo: Bean Lifecycle & Scopes
Bài này có giúp bạn hiểu bản chất không?
Hỏi đáp về bài này
Chưa có câu hỏi
Có gì chưa rõ trong bài? Đặt câu hỏi đầu tiên — câu trả lời từ cộng đồng giúp bạn (và người sau).
Đặt câu hỏi đầu tiên