liveBook · Manning

Constructor injection

Constructor injection is a dependency-injection technique in which an object's dependencies or required values are supplied through its constructor when it is created.

How 3 Manning books explain it

Spring Start Here, Second Edition

Constructor injection is a dependency-injection technique that supplies an object's dependencies through its constructor parameters.

How it is used
Uses constructor parameters to connect collaborating beans when Spring creates objects.
How deep it goes
Explains constructor syntax, final fields, testing benefits, and annotation omission, but does not detail injection internals.
Examples
Demonstrates a Person receiving a Parrot and a CommentService receiving repository and notification dependencies.
What you can do after
Build it from scratch by defining a constructor that accepts and assigns the required dependencies.

Read how Spring Start Here, Second Edition explains constructor injection →

ASP.NET Core in Action, 3e

Constructor injection is a dependency injection technique in which required dependencies are supplied through an object's constructor.

How it is used
Uses explicit constructor dependencies to keep application code loosely coupled while the DI container handles object creation.
How deep it goes
Explains constructor inspection and contrasts injection with manually creating a nested dependency graph, without detailing container configuration.
Examples
Demonstrates EmailSender receiving MessageFactory and NetworkClient, then being supplied to a RegisterUser endpoint.
What you can do after
Apply it through ASP.NET Core's built-in DI container and endpoint handler injection.

Read how ASP.NET Core in Action, 3e explains constructor injection →

Test Driven: Practical TDD and Acceptance TDD for Java Developers

A form of dependency injection in which dependencies are supplied through an object's constructor.

How it is used
Explains how to supply dependencies while supporting testable code and considers the cost of wiring them in production.
How deep it goes
Defines constructor injection and compares its tradeoffs with setter and field injection, without detailing a wiring implementation.
Examples
Uses a LoginServlet and AuthenticationService to illustrate why a servlet container complicates constructor injection.
What you can do after
Evaluate constructor injection’s configuration benefits and limitations, including constraints imposed by framework-created objects.

Read how Test Driven: Practical TDD and Acceptance TDD for Java Developers explains constructor injection →

Also covered in

What to know first

Related concepts

Questions about constructor injection

What is constructor injection?

Constructor injection is a dependency-injection technique in which an object's dependencies or required values are supplied through its constructor when it is created.

Which Manning books explain constructor injection?

Spring Start Here, Second Edition, ASP.NET Core in Action, 3e and Test Driven: Practical TDD and Acceptance TDD for Java Developers.

What should I know before learning constructor injection?

Manning books that teach constructor injection usually introduce dependency injection and inversion of control first.

What is the difference between constructor injection and field injection?

Constructor injection is a dependency-injection technique in which an object's dependencies or required values are supplied through its constructor when it is created. A dependency-injection technique that supplies a dependency by assigning it directly to an object’s field.

What is constructor injection a kind of?

Constructor injection is a kind of dependency injection. Dependency injection is a software design technique in which a component receives its dependencies from outside rather than creating or locating them internally.

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.