liveBook · Manning

Dependency injection

As explained in Clean Applications with Hexagonal Architecture

Dependency injection is a design technique in which an object's dependencies are supplied by external configuration or infrastructure.

Dependency injection connects an abstraction to a concrete implementation at runtime. In a hexagonal system, the assembly module uses Spring Boot to discover beans, match interfaces to implementations, and inject dependencies. section 6.2

Keep framework wiring in the assembly module, not the application core. For construction that needs an external service, use a standalone factory and inject that service so the factory remains testable with mocks. section 6.2 section 3.6

Listing 3.14 Using factories for complex construction of entities
public class ArticleFactory { #A
  private final ArticleIdGenerator idGenerator;
 
  public ArticleFactory(final ArticleIdGenerator idGenerator) {
    this.idGenerator = idGenerator; #B
  }
 
  public Validation<Seq<DomainError>, Article> create( #C
    final Title title, final Content content, final AuthorId authorId) {
    final ArticleId id = this.idGenerator.next(); #D
    return Article.validateThenCreate(id, title, content, authorId);
  } #E
}

ArticleFactory receives an ArticleIdGenerator through its constructor and uses it to create an article. section 3.6

How Clean Applications with Hexagonal Architecture covers it

How deep it goes
Explains assembly-module visibility, bean discovery, interface matching, and injection while keeping framework wiring outside the core.
Examples
Uses Spring assembly to connect ArticleRepository to its adapter and injects ArticleIdGenerator into ArticleFactory.
What it assumes
Assumes familiarity with layered architectures, SOLID principles, and unit testing, without requiring Spring Boot internals expertise.
What you can do after
Operate Spring Boot assembly to resolve interfaces to implementations and inject external services into factories.
How it connects
Connects hexagonal architecture, ports and adapters, Spring Boot, Maven modules, and factories.

Also explained in

Compare how 28 books explain dependency injection →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clean Applications with Hexagonal Architecture. The section links point to the parts of the book it draws on.