liveBook · Manning

Dependency injection

As explained in Spring Start Here, Second Edition

Dependency injection is a pattern in which an object's dependencies are supplied by an external component rather than constructed by the object itself.

Objects often need other objects to do their work. With dependency injection (DI), an object receives a dependency from an external component rather than creating it itself. In Spring, the framework supplies references between managed objects, called beans, from the Spring context. DI is an application of inversion of control: the framework takes responsibility for providing dependencies. section 3.0 section 3.6

A person and a parrot

A Person bean needs a Parrot. Instead of constructing the parrot inside Person, a configuration method declares a Parrot parameter. Spring finds a matching bean in its context and supplies it when calling the method; the method then assigns it to the person. section 3.1.2

Listing 3.5 Injecting bean dependencies by using parameters of the methods
@Configuration
public class ProjectConfig {
 
  @Bean
  public Parrot parrot() {
    Parrot p = new Parrot();
    p.setName("Koko");
    return p;
  }
 
  @Bean
  public Person person(Parrot parrot) {  #A
    Person p = new Person();
    p.setName("Ella");
    p.setParrot(parrot);
    return p;
  }
}

The person method receives the parrot bean as a parameter and sets it on the new person. section 3.1.2

Another way to connect beans in a configuration class is to call one @Bean method from another. Parameter injection instead lets Spring provide the dependency, whether the bean was defined with @Bean or a stereotype annotation such as @Component. section 3.1.2 section 3.6

In a Spring-managed class, @Autowired marks a dependency for injection. Spring supports three places to receive it:

  • A field: often seen in examples and proofs of concept. section 3.2
  • A constructor parameter: generally preferred for readability, testability, and immutability. section 3.2 section 3.6
  • A setter: rarely used in production-ready code. section 3.2

If several beans match a dependency, Spring needs help choosing. Mark one @Primary to make it the default, or use @Qualifier to select a specific bean by name. Without a way to resolve the choice, injection can fail with an exception. section 3.4

A dependency can also be expressed as a Java interface. Spring then looks for a bean implementing that interface. This separates the behavior a class needs from its implementation, making the application easier to change and maintain. If several beans implement the interface, use @Primary or @Qualifier to select one. section 4.0 section 4.5

A circular dependency occurs when beans depend on each other during creation. It usually signals a design problem that should be refactored. section 3.6

How Spring Start Here, Second Edition covers it

How deep it goes
Explains configuration-method, field, constructor, and setter injection, including ambiguity resolution and circular dependencies.
Examples
Uses Person and Parrot beans to demonstrate method-parameter injection and selection among multiple beans.
What it assumes
Introduces injection with simple object examples while building on familiarity with the Spring context and bean registration.
What you can do after
Apply it via Spring to wire beans, select among matching candidates, and inject interface implementations.
How it connects
Connects dependency injection to inversion of control, interfaces, decoupling, and circular dependencies.

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