liveBook · Manning

Chapter 3: The Spring context: Wiring beans

What you will learn in chapter 3 of Spring Start Here, Second Edition

The chapter introduces how Spring-managed beans collaborate by receiving references to one another, and names two basic ways to establish those relationships.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduces how Spring-managed beans collaborate by receiving references to one another, and names two basic ways to establish those relationships.

Key terms: beans, Spring context, getBean(), dependency injection, wiring, autowiring, method parameter

3.1 Implementing relationships among beans defined in a configuration file

The section sets up Person and Parrot beans to show that adding both to the Spring context does not, by itself, establish a relationship between them.

Key terms: Spring context, bean, relationship, has-A, @Bean annotation, configuration class

3.1.1 Wiring the beans using a direct method call between the @Bean methods

This section shows how a direct call between @Bean methods wires two beans and explains why that call does not create a duplicate bean.

Key terms: wiring, configuration class, @Bean, Spring context, parrot bean

3.1.2 Wiring the beans using the @Bean annotated method’s parameters

This section shows how Spring wires beans through @Bean method parameters and explains that this is dependency injection under the IoC principle.

Key terms: @Bean, parameter, Spring context, @Component, Parrot, dependency injection (DI), inversion of control (IoC), inject

3.2 Using the @Autowired annotation to inject beans

Introduces @Autowired as a way to declare a bean’s dependency in its own class and identifies three injection locations.

Key terms: @Autowired, Spring context, dependency, field, constructor parameters, setter

3.2.1 Using @Autowired to inject the values through the class fields

This section shows how @Autowired injects a bean into a field and explains why field injection is usually avoided in production code.

Key terms: @Autowired, field injection, @Component, @ComponentScan, Spring context, final, immutability, unit tests

3.2.2 Using @Autowired to inject the values through the constructor

This section shows how to inject a bean through a constructor instead of a field and explains why constructor injection is often preferred.

Key terms: constructor injection, @Autowired, bean, Spring context, final, field injection, unit tests

3.2.3 Using dependency injection through the setter

This subsection shows that Spring can inject a dependency through an @Autowired setter, while explaining why the author does not recommend the approach.

Key terms: setter injection, @Autowired, Person class, setter, dependency injection, final

3.3 Dealing with circular dependencies

This section explains how circular bean dependencies prevent Spring from creating beans and how to recognize and remove them.

Key terms: circular dependency, bean, deadlock, exception, Person, Parrot

3.4 Choosing from multiple beans in the Spring context

This section explains how to choose a bean for injection when the Spring context contains multiple beans of the same type.

Key terms: Spring context, @Primary, @Qualifier, injection, parrot2, @Bean, @Component, @ComponentScan, constructor’s parameter

3.5 Questions

This section lists questions for readers to check their understanding of the chapter; answers are provided in Appendix A.

3.6 Summary

The section summarizes how Spring wires collaborating beans through dependency injection and how to handle common wiring choices and problems.

Where this is used later

Concepts explained in this chapter

← Chapter 2: The Spring context: Defining beans Chapter 4: The Spring context: Using abstractions →

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.