liveBook · Manning

Dependency injection

As explained in Kafka Streams in Action, Second Edition

Dependency injection is a design technique in which an object's required dependencies are supplied by external configuration or a framework rather than created by the object itself.

Dependency injection supplies an object’s collaborators from outside the object rather than having it construct them. It is a way to apply inversion of control: application code depends on interfaces, while a container wires concrete implementations together. Constructor, setter, and field injection are available; constructor injection makes required collaborators explicit. section 12.0 section 12.1

Keeping a component behind an interface limits its knowledge to a contract. That lets implementations change without rewriting the dependent class and makes tests simpler because a mock implementation can be supplied in place of a real one. section 12.1

Inject a network client into a payment processor

A PaymentProcessor receives a NetworkClient through its constructor, then calls receive and send without knowing how network communication is implemented. Marking the processor and a SecureNetworkClient implementation as components lets the container discover them and satisfy the constructor dependency. section 12.1 section 12.1

Wire Kafka infrastructure into a loan processor

A loan-processing component is discovered with @Component. Its output-topic names come from configuration properties, while a KafkaTemplate is requested as a constructor parameter with @Autowired and created by the container at startup. The processor can then listen for loans and publish approval, rejection, or quality-assurance records. section 12.2.1 section 12.2.1

Dependency injection can be used independently of broader Spring Boot conveniences. For Kafka Streams, one approach injects a StreamsBuilder into a topology-building method; another injects KafkaStreamsConfiguration while application code explicitly creates, starts, and stops KafkaStreams for greater control. section 12.4 section 12.3

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Defines IoC and injection styles, then demonstrates constructor and property wiring in Java components; detailed container internals are not developed.
Examples
Demonstrates a PaymentProcessor with NetworkClient and loan processors receiving KafkaTemplate, StreamsBuilder, and KafkaStreamsConfiguration.
What it assumes
Introduces IoC through a payment-processing example while assuming readers can follow Java classes, interfaces, constructors, and annotations.
What you can do after
Apply it via Spring annotations to wire collaborators, configuration properties, and Kafka infrastructure into application components.
How it connects
Connects inversion of control, interfaces, Spring containers, Spring Boot, KafkaTemplate, Kafka listeners, and Kafka Streams.

Also explained in

Compare how 21 books explain dependency injection →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kafka Streams in Action, Second Edition. The section links point to the parts of the book it draws on.