IoC, constructor injection and bean definitions
Prerequisites: 01-ecosystem
Who creates and connects collaborators?
Goal & mental model verified
Inversion of control means the container constructs and connects collaborators. Dependency injection exposes what an object needs. Component scanning and @Bean methods are two ways to register bean definitions.
[S16] [S20]Worked example · illustrative, not executed synthesis
class QuoteService {
private final PricingPort pricing;
QuoteService(PricingPort pricing) { this.pricing = pricing; }
}
// Register QuoteService and a PricingPort implementation as beans.[S16] [S20]Engineering decision synthesis
Use constructor injection for required collaborators. Use @Bean for third-party objects or explicit assembly; use qualifiers when multiple candidates represent meaningful alternatives.
[S16] [S20]Pitfall & diagnosis synthesis
Field injection hides required dependencies and complicates plain construction. Circular dependencies often signal tangled responsibilities; adding lazy references can conceal the design problem.
[S16] [S20]Improve & validate synthesis
Construct business services directly in unit tests. Inspect candidate ambiguity and configuration boundaries before widening component scans.
[S16] [S20]Check yourself: Why inject an interface?
It names a replaceable contract; the container supplies the selected implementation.