Part 8 · Fundamentals

IoC, constructor injection and bean definitions

Prerequisites: 01-ecosystem

Who creates and connects collaborators?

ApplicationContext: bean definitions; Pricing adapter: implements PricingPort; QuoteService: requires PricingPort. Connections: ApplicationContext to Pricing adapter (construct); ApplicationContext to QuoteService (construct); Pricing adapter to QuoteService (inject reference)
Arrows distinguish construction from injecting an existing collaborator. Business calls are omitted. [S16] [S20]

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]
Keep this: The container should assemble your design, not become your design.
Check yourself: Why inject an interface?

It names a replaceable contract; the container supplies the selected implementation.

Sources & further reading

  1. [S16] Dependency injection

    Spring project maintainers · documentation · accessed 2026-10-09 · Documentation retrieved 2026-10-09

    Supports: Constructor injection; Dependency resolution

    Read the linked section to validate the mechanism and its version-specific constraints.

  2. [S20] Bean and Configuration

    Spring project maintainers · documentation · accessed 2026-10-09 · Documentation retrieved 2026-10-09

    Supports: Java bean definitions; Configuration classes

    Read the linked section to validate the mechanism and its version-specific constraints.