3 The Spring context: Wiring beans

 

This chapter covers

  • Establishing relationships among beans
  • Using dependency injection
  • Accessing the beans from the Spring context through dependency injection

In chapter 2, we discussed the Spring context: the place in the app’s memory where we add the object instances we want Spring to manage. Because Spring uses the IoC principle, as we discussed in chapter 1, we need to tell Spring which objects of our app it needs to control. Spring needs control over some of the objects of our app to augment them with the capabilities it provides. In chapter 2, you learned multiple ways to add object instances to the Spring context. You also learned that we add these instances (beans) into the Spring context to make Spring aware of them.

3.1 Implementing relationships among beans defined in the configuration file

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

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

3.2 Using the @Autowired annotation to inject beans

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

3.2.2 Using @Autowired to inject the values through the constructor

3.2.3 Using dependency injection through the setter

3.3 Dealing with circular dependencies

3.4 Choosing from multiple beans in the Spring context

Summary