@Component, @Autowired, @Bean, etc.Inversion of Control:
Instead of classes creating their dependencies, the container injects them.
Dependency Injection:
A design pattern where an object’s dependencies are provided externally.
class Service {
Repo repo = new Repo(); // tight coupling
}
@Component
class Service {
private final Repo repo;
@Autowired
Service(Repo repo) { this.repo = repo; }
}
| Phase | Hook |
|---|---|
| Instantiation | Constructor |
| Dependency Injection | @Autowired |
| Initialization | @PostConstruct, InitializingBean.afterPropertiesSet() |
| Destruction | @PreDestroy, DisposableBean.destroy() |