SteelReed

Tìm kiếm

Tìm bài viết, câu hỏi…

Spring BootCâu 7 / 26

Middle

Vòng đời một bean gồm những bước nào? BeanPostProcessor và BeanFactoryPostProcessor khác nhau thế nào?

Trả lời ngắn

Bean đi từ definition → khởi tạo → inject → callback → postProcessAfterInitialization (nơi tạo proxy AOP) → sẵn sàng → @PreDestroy. BeanFactoryPostProcessor sửa metadata trước khi có bean; BeanPostProcessor xử lý từng instance.

BeanFactoryPostProcessorBean definitionKhởi tạo + injectCallback *AwareBPP before-init@PostConstructBPP after-inittạo proxy AOPSẵn sàng, rồi @PreDestroy

3 ý phải nói:

  1. Thứ tự: definition → instance → inject → Aware → before-init → @PostConstruct → after-init (proxy).
  2. BFPP sửa definition trước khi có bean nào; BPP sửa hoặc bọc từng instance.
  3. @PostConstruct chạy trên object gốc, chưa có proxy; việc nặng đặt ở runner hoặc ApplicationReadyEvent.

Đọc tiếp phần giải thích sâu

Tạo tài khoản miễn phí để đọc phần giải thích chi tiết, câu hỏi nối tiếp, đánh dấu câu đã học và ôn lại theo lịch.