Container Internals

Mổ bên trong IoC container Spring: BeanFactory vs ApplicationContext, refresh() 12 bước, BeanDefinition + BeanFactoryPostProcessor, Environment/PropertySource, Resource + @SpringBootApplication. Hiểu data structure thật để debug startup không hoang mang.

8 bài · ~100 phútMiễn phí

Nội dung

Danh sách bài học

  1. 01

    Container Internals — tổng quan

    Mổ bên trong IoC container Spring qua 5 bài atomic: hai interface, refresh() 12 bước, BeanDefinition + BFPP, Environment, Resource + @SpringBootApplication.

    ~4 phút
  2. 02

    BeanFactory vs ApplicationContext — container thực sự là cái gì bên dưới

    Container Spring không phải hộp đen. Bài này bóc đúng MỘT thứ: hai interface BeanFactory và ApplicationContext khác nhau ra sao, và bên dưới chúng được implement bằng data structure nào — singletonObjects map, beanDefinitionMap, và cơ chế delegation khiến ApplicationContext 'là' một BeanFactory.

    ~12 phút
  3. 03

    refresh() — 12 bước biến config thành runtime container

    refresh() là method quan trọng nhất của ApplicationContext. Bài này đi sâu 12 bước trong AbstractApplicationContext.refresh(): từ load BeanDefinition đến eager instantiate singleton, chia 3 giai đoạn rõ ràng. Hiểu cơ chế này là chìa khoá debug mọi startup error — BeanCreationException, NoSuchBeanDefinitionException, BeanCurrentlyInCreationException — vì mỗi loại lỗi ứng với đúng một bước.

    ~12 phút
  4. 04

    BeanDefinition & BeanFactoryPostProcessor — metadata bean và pha can thiệp trước instantiate

    BeanDefinition là bản mô tả metadata của bean (class, scope, dependencies) — chưa phải object thật. BeanFactoryPostProcessor (BFPP) có thể chỉnh sửa metadata này trước khi container instantiate bất kỳ bean nào. Bài này đào sâu hai khái niệm đó, phân biệt BFPP vs BPP, và giải thích vì sao tách pha metadata khỏi instance là nền móng của autoconfig Spring.

    ~12 phút
  5. 05

    Environment & PropertySource — externalized config và thứ tự ưu tiên

    Environment là 1 trong 4 capability của ApplicationContext: quản lý property (từ command line, env var, application.properties, ...) và profile (dev/prod/test). Bài này bóc cơ chế PropertySource ordering, relax binding, @Value/${...}/SpEL — giải thích vì sao command line override mọi thứ và vì sao env var tồn tại song song system property.

    ~12 phút
  6. 06

    Resource abstraction và @SpringBootApplication — nạp resource và bootstrap container

    Bài này bóc 2 thứ liên quan chặt đến nhau: Resource interface trừu tượng hoá classpath/file/URL thành một API duy nhất, và @SpringBootApplication = 3 annotation hợp lại bootstrap container không magic. Hiểu 2 cơ chế này giải thích vì sao classpath: hay dùng nhất khi đóng gói jar, vì sao đặt App ở root package, và vì sao Spring Boot chỉ cần 1 context duy nhất.

    ~13 phút
  7. 07

    Mini-challenge: tự build mini IoC container 80 dòng

    Bài thực hành khép lại Module 1 — code 1 IoC container đơn giản đủ tính năng: scan @Component, resolve dependency qua constructor, lifecycle @PostConstruct. Chứng minh Spring không phải magic — chỉ là reflection + topological sort + map cache.

    ~30 phút
  8. 08

    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.

    ~5 phút