Constructor and Setter Injection
Learn how beans can be wired in using constructor and setter injection.
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.