liveBook · Manning

Dependency injection

As explained in Spring in Action, Sixth Edition

A framework that supplies an object's dependencies from outside the object rather than requiring the object to construct them.

Dependency injection (DI) lets application components receive the objects they need instead of constructing and managing those objects themselves. In Spring, the application context acts as a container: it creates components called beans, manages their lifecycles, and wires them together into a working application. section 1.1

A typical Spring dependency relationship works like this:

  • Define components such as an inventory service and a product service. section 1.1
  • Declare that the product service depends on the inventory service. section 1.1
  • Let the Spring application context create both beans and provide the inventory service through a constructor argument or property accessor. section 1.1

Wiring services

An inventory service fetches inventory levels, while a product service supplies product information. Because complete product information requires inventory data, Spring constructs the product service with the inventory service it manages. The same relationship can be expressed with XML bean definitions or Java configuration using @Configuration and @Bean methods. section 1.1

Figure 1.1 Application components are managed and injected into each other by the Spring application context.

The relationship diagram shows the Spring application context managing application components and injecting them into one another. section 1.1

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains container-managed beans, constructor arguments, and property accessors through a simple service relationship.
Examples
Uses inventory and product services, XML bean definitions, Java @Configuration, and @Bean methods.
What it assumes
Assumes cooperating application components, while defining beans and the application context from basic terms.
What you can do after
Apply it via Spring by defining service beans and wiring a product service to an inventory service.
How it connects
Connects dependency injection to beans, the Spring application context, constructors, property accessors, and Spring MVC.

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 Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.