@Component | @Bean | |
|---|---|---|
| Đặt ở | Class | Method trong @Configuration |
| Tạo bean | Spring new qua default constructor | Method return object |
| Kiểm soát init | Hạn chế | Linh hoạt — logic tuỳ ý |
| Discovery | Component scan | Khai báo tường minh |
Dùng @Bean (method trong @Configuration return object) cho: class thư viện ngoài không gắn @Component được (vd RestTemplate, ObjectMapper), init phức tạp/cần cấu hình (timeout, builder), conditional bean (@ConditionalOnProperty), cần nhiều instance khác nhau của cùng class.
Dùng @Component (và các biến thể @Service/@Repository/@Controller) cho class trong app của bạn, init đơn giản.
Cả 2 mặc định singleton scope.