liveBook · Manning

Autowiring

As explained in Spring Start Here, Second Edition

Autowiring is the technique of having Spring automatically resolve and establish dependencies among managed beans.

Autowiring lets Spring provide references between managed beans, so objects can collaborate without manually retrieving each other. Unlike wiring, which links beans by directly calling the methods that declare them, autowiring uses Spring’s dependency injection. section 3.0

Figure 3.1 Having two beans in the Spring context, we want to establish a relationship between them. We do this so that one object can then delegate to the other in the implementation of their responsibilities. You can do this using a wiring approach, which implies directly calling the methods that declare the beans to establish the link between them, or through auto-wiring. You use the framework’s dependency injection capabilities.

See the two ways to establish a relationship between beans: direct method calls and Spring-provided dependencies. section 3.1

A person and a parrot

Register a Person bean and a Parrot bean, then establish a relationship so the person can use the parrot. Before the relationship is configured, the person’s parrot reference is null. The example separates adding beans to the context from linking them. section 3.1

Spring can supply dependencies in several places:

  • Constructor, field, or setter injection using @Autowired; constructor injection is the recommended approach, while field injection is shown for demonstration. section 4.2.1
  • Parameters of methods annotated with @Bean, which Spring uses to create beans in the context. section 4.2.1

How Spring Start Here, Second Edition covers it

How deep it goes
Explains bean registration and dependency linking with small examples, without detailing resolution internals.
Examples
Uses a Person–Parrot relationship and a CommentService with repository and notification dependencies.
What it assumes
Builds from adding beans to the Spring context before establishing relationships among them.
What you can do after
Apply it by letting Spring supply bean references through constructor, field, setter, or @Bean method parameters.
How it connects
Connects autowiring with dependency injection, Spring beans, and constructor, field, and setter injection.

Also explained in

Compare how 2 books explain autowiring →

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.