Chapter 3: The Spring context: Wiring beans
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
- Relationships Between Spring Beans. Explain why beans need references to other beans and how Spring can provide those references instead of the application retrieving beans with getBean().
- Wiring and Autowiring Distinction. Distinguish the two relationship-establishing approaches named here: linking beans by calling the methods that created them, and having Spring provide a value through a method parameter.
- Bean presence versus relationship. Explain why defining both a Person bean and a Parrot bean does not make the person own the parrot, and describe the relationship that still needs to be established.
- Direct method call wiring. Describe how calling the parrot() method from the person() method in a configuration class links the person bean to the parrot bean.
- Spring managed bean reuse. Explain what Spring does when one @Bean method calls another, depending on whether the requested bean already exists, and how the example checks for duplicate creation.
- Bean wiring through parameters. Explain how a parameter on a @Bean method lets Spring supply a bean from its context, using the person() and Parrot example. Distinguish this from directly calling another @Bean method.
- Dependency injection and IoC. Explain why Spring supplying a value to the person() parameter is called dependency injection and how the example illustrates inversion of control.
- Autowired dependency declaration. Explain that @Autowired marks where an object needs Spring to inject a value from the context, declaring the dependency in the class that needs it.
- Field injection with Autowired. Describe how annotating Person’s parrot field with @Autowired tells Spring to inject a Parrot bean from its context, and how the example makes both beans available.
- Field injection production drawbacks. Explain why the section discourages field injection in production despite its simplicity and frequent use in examples and tests.
- Constructor based bean injection. Describe how Spring supplies a bean through a constructor and how the Person example changes from field injection to constructor injection. State when @Autowired can be omitted.
- Setter-Based Dependency Injection. Describe how the example uses an @Autowired setter in the Person class to inject a dependency, with the same app output as the earlier examples.
- Circular bean dependency deadlock. Explain why Spring cannot create two beans when each bean’s creation requires the other.
- Circular dependency diagnosis and remedy. Describe what the exception indicates in the Person–Parrot example and what to change when a circular dependency occurs.
- Choosing Among Same Type Beans. Explain what happens when Spring must inject one of several beans of the same type: it selects a primary bean, can be directed with @Qualifier, or fails if neither resolves the choice.
What it builds on
- Summary (chapter 2) · The reader has encountered beans as objects registered in the Spring context.
- Using the @Bean annotation to create beans (chapter 2) · Introduces methods used to create beans.
- Using stereotype annotations to create beans (chapter 2) · Uses stereotype annotations to create beans.
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.
- Relationships Between Spring Beans: Explain why beans need references to other beans and how Spring can provide those references instead of the application retrieving beans with getBean().
Why it matters: Bean relationships let Spring-managed objects work together without each object manually looking up its collaborators. - Wiring and Autowiring Distinction: Distinguish the two relationship-establishing approaches named here: linking beans by calling the methods that created them, and having Spring provide a value through a method parameter.
Why it matters: These names identify the two approaches the chapter will use to connect beans.
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.
- Bean presence versus relationship: Explain why defining both a Person bean and a Parrot bean does not make the person own the parrot, and describe the relationship that still needs to be established.
Why it matters: It helps you distinguish creating beans from making them work together. - Unmodifiable class bean registration: Explain why a configuration method annotated with @Bean is useful when the class of an object cannot be changed.
Why it matters: It explains why defining beans in a configuration class is useful beyond classes you write yourself.
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.
- Direct method call wiring: Describe how calling the parrot() method from the person() method in a configuration class links the person bean to the parrot bean.
Why it matters: It gives you a straightforward way to connect beans defined in a configuration class. - Spring managed bean reuse: Explain what Spring does when one @Bean method calls another, depending on whether the requested bean already exists, and how the example checks for duplicate creation.
Why it matters: It explains why wiring through a method call does not accidentally create a second parrot.
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.
- Bean wiring through parameters: Explain how a parameter on a @Bean method lets Spring supply a bean from its context, using the person() and Parrot example. Distinguish this from directly calling another @Bean method.
Why it matters: This gives you a way to connect beans without calling the method that defines the dependency. - Dependency injection and IoC: Explain why Spring supplying a value to the person() parameter is called dependency injection and how the example illustrates inversion of control.
Why it matters: The example gives a concrete meaning to DI and shows why letting Spring supply dependencies can reduce application code.
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.
- Autowired dependency declaration: Explain that @Autowired marks where an object needs Spring to inject a value from the context, declaring the dependency in the class that needs it.
Why it matters: It helps you recognize where a class declares a dependency supplied by Spring. - Autowired injection locations: Distinguish field, constructor-parameter, and setter injection as the three locations introduced for @Autowired, including how commonly the section says each is used.
Why it matters: These distinctions orient you before the section examines each injection approach in detail.
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.
- Field injection with Autowired: Describe how annotating Person’s parrot field with @Autowired tells Spring to inject a Parrot bean from its context, and how the example makes both beans available.
Why it matters: It connects bean creation and discovery to the reference injected into a field. - Field injection production drawbacks: Explain why the section discourages field injection in production despite its simplicity and frequent use in examples and tests.
Why it matters: These trade-offs explain why the simplest injection style is not necessarily the best production choice.
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.
- Constructor based bean injection: Describe how Spring supplies a bean through a constructor and how the Person example changes from field injection to constructor injection. State when @Autowired can be omitted.
Why it matters: This gives you a way to connect beans without injecting directly into fields. - Reasons for constructor injection: Explain why the section recommends constructor injection over field injection for production code and tests.
Why it matters: It explains why the recommended approach helps both object design and testing.
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.
- Setter-Based Dependency Injection: Describe how the example uses an @Autowired setter in the Person class to inject a dependency, with the same app output as the earlier examples.
Why it matters: You can recognize setter injection when you encounter it in a Spring app. - Setter Injection Drawbacks: Explain why the author does not recommend setter injection despite noting that it appears in some older apps.
Why it matters: These drawbacks explain why recognizing setter injection is useful even if you do not choose it for new code.
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.
- Circular bean dependency deadlock: Explain why Spring cannot create two beans when each bean’s creation requires the other.
Why it matters: Recognizing the two-way dependency helps explain why bean creation fails. - Circular dependency diagnosis and remedy: Describe what the exception indicates in the Person–Parrot example and what to change when a circular dependency occurs.
Why it matters: The exception points you to the classes whose dependency design needs changing.
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.
- Choosing Among Same Type Beans: Explain what happens when Spring must inject one of several beans of the same type: it selects a primary bean, can be directed with @Qualifier, or fails if neither resolves the choice.
Why it matters: These rules help you predict which bean Spring injects and diagnose an ambiguous-bean error. - Qualified Constructor Injection Example: Describe how the example directs Spring to inject the parrot2 bean into a Person constructor parameter, and identify why the example uses both @Bean definitions and component scanning.
Why it matters: The example connects the bean-selection rule to a concrete constructor-injection setup.
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
- Using the singleton bean scope (chapter 5) · Provides an example of a bean instance being reused from the Spring context.
- Using dependency injection with abstractions (chapter 4) · Applies dependency injection when working with abstractions.
- (chapter intro) (chapter 4) · Introduces using Spring to inject beans.
- Deciding which objects should be part of the Spring context (chapter 4) · The use of dependency injection to connect beans.
- Implementing unit tests in Spring apps (chapter 15) · Connects dependency management at object initialization to writing unit tests.
- Singleton beans in real-world scenarios (chapter 5) · Injecting dependencies through a constructor.
- Using Spring Data JDBC (chapter 14) · injecting a bean through a constructor
- Choosing what to auto-wire from multiple implementations of an abstraction (chapter 4) · Choosing what to inject from multiple implementations builds on resolving multiple candidate beans.
Concepts explained in this chapter
- dependency injectionDependency injection is a pattern in which an object's dependencies are supplied by an external component rather than constructed by the object itself.
- @AutowiredThe Spring @Autowired annotation marks a constructor, field, or method for automatic dependency injection.
- @BeanA Spring annotation that marks a method whose returned object is registered as a bean.
- Spring beanAn object instance created, configured, and managed by the Spring IoC container.
- constructor injectionConstructor injection is a dependency-injection technique that supplies an object's dependencies through its constructor parameters.
- spring-contextThe spring-context module is a Spring Framework module that provides the application context and related infrastructure.
- autowiringAutowiring is the technique of having Spring automatically resolve and establish dependencies among managed beans.
- wiringWiring is the direct establishment of relationships between Spring-managed beans by calling the methods that create them.
- qualifier annotationThe @Qualifier annotation identifies which bean should be selected when multiple beans match an injection point.
- field injectionField injection is a dependency-injection technique that assigns a dependency directly to an object's field.
- configuration classA configuration class is a Spring class annotated with @Configuration that declares instructions for configuring the Spring context.
- @ComponentScan annotationA Spring annotation that configures package scanning for classes marked with component stereotype annotations.
- component annotationThe component annotation is a Spring annotation that marks a class for automatic detection and registration as a bean.
- stereotype annotationA stereotype annotation is a Spring annotation that declares an application class's role so Spring can detect and manage it as a component.
- immutabilityImmutability is the property that an object's state cannot be changed after initialization.
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.
