Constructor and Setter Injection
Explore the methods of dependency injection in Spring framework, focusing on constructor and setter injection techniques. Understand how to use annotations like @Autowired and @Qualifier to control bean wiring, and learn the benefits and differences between these approaches for building robust applications.
We'll cover the following...
Spring framework identifies dependencies and wires them in. Spring framework gives the developer control over how beans are wired in. There are a variety of options to choose from. We will focus on constructor injection and setter injection.
For the code example shown in this lesson, we have created a sub-package called lesson7 inside the package io.datajek.spring.basics.movierecommendersystem.The package contains MovieRecommenderSystemApplication.java, RecommenderImplementation.java, Filter.java, ContentBasedFilter.java, and CollaborativeFilter.java files from the previous lesson.
To show the two different ways of dependency injection, we will create a copy of the RecommenderImplementation class and call it RecommenderImplementation2. One will be used to show constructor injection while the other will demonstrate setter injection.
We will use the @Autowired annotation at different places in the code to direct Spring which injection type to use.