Dependency Injection in Spring

hemasai jammana
3 min readMar 28, 2023

--

In the earlier article, we have seen the spring IOC container and how many ways we can configure the container, and how the container is helpful in spring. In this article, we will be learning what is Dependency Injection or Inversion of Control and how can we achieve that.

Spring

Dependency Injection is the process of assigning dependent objects to target objects by loading both classes and creating the objects for both classes.
The classes/objects which use the other class services are called target classes.
The class that acts like a helper class to the main/ target class is called the dependent class.
The whole process of creating and injecting the bean is done by the container itself and it is generally inverse, hence it is an Inversion Of Control

Example:- vehicle to engine, the vehicle is the target class and the engine is the dependent class.

Dependency Injection can be achieved in the following ways:
1. Constructor Injection
2. Setter Injection

Constructor Injection:
This type of injection is done by using the constructor with a number of arguments for each dependency.

Example 1:

// target class
public class College {
private Student student; // dependent class

// constructor of college.
public College(Student student) {
this.student = student;
}
}
Example 2:

// target class
public class College {
private Student student; // dependent class

private Course course; // dependent class

// constructor of college.
public College(Student student, Course course) {
this.student = student;
this.course = course;
}
}

In the above example, only one is the dependent class and in the configuration file(XML file) we will provide only one dependency as follows:

    <!--    Configuring dependent bean-->
<bean id="student" class="src.model.Student"/>
<!-- configuration target bean-->
<bean id="university" class="src.University">
<constructor-arg ref="student"/>
</bean>

In example 2, we have 2 dependencies and we will provide references as follows:

    <!--    Configuring dependent bean-->
<bean id="student" class="src.model.Student"/>
<bean id="course" class="src.model.Course"/>

<!-- configuration target bean-->
<bean id="university" class="src.University">
<constructor-arg ref="student"/>
<constructor-arg ref="course"/>
</bean>

If we have primitive types to be injected then we have the below ways, we can do using:
1. type attribute:
In this, we will configure the bean using the type and value for that particular bean.

    <!--    Configuring dependent bean-->
<bean id="student" class="src.model.Student"/>
<bean id="course" class="src.model.Course"/>

<!-- configuration target bean-->
<bean id="university" class="src.University">
<constructor-arg type="int" value="25000"/>
<!-- type attribute is used if more than one arguments -->
</bean>

2. index attribute:
In this, we will configure the bean using the index and value for that particular bean.

    <!--    Configuring dependent bean-->
<bean id="student" class="src.model.Student"/>
<bean id="course" class="src.model.Course"/>

<!-- configuration target bean-->
<bean id="university" class="src.University">
<constructor-arg index="0" value="25000"/>
<!-- index attribute is used specify the arguments-->
</bean>

3. name attribute:
In this, we will configure the bean using the index and value for that particular bean.

    <!--    Configuring dependent bean-->
<bean id="student" class="src.model.Student"/>
<bean id="course" class="src.model.Course"/>

<!-- configuration target bean-->
<bean id="university" class="src.University">
<constructor-arg name="founded" value="1990"/>
<!-- name attribute is used to give data to exact argument-->
</bean>

Setter Injection:
This type of injection is done by calling the setter method after invoking the zero arguments constructor.

Example 1: 

// target class
public class College {
private Student student; // dependent class

private Course course; // dependent class
private int integerProperty;

public void setStudent(Student student) {
this.student = student;
}

public void setCourse(Course course) {
this.course = course;
}

public void setIntegerProperty(int integerProperty) {
this.integerProperty = integerProperty;
}
}

In the above example, we are using the setter to inject the bean and one integer value to be injected, for the above injection XML file will be as follows:

    <!--    Configuring dependent bean-->
<bean id="studentBean" class="src.model.Student"/>
<bean id="courseBean" class="src.model.Course"/>

<!-- configuration target bean-->
<bean id="university" class="src.University">
<property name="student" ref="studentBean"/>
<property name="course" ref="courseBean"/>
<property name="integerProperty" value="1"/>
</bean>
constructor vs setter injection

Please do follow for more articles and learn with me.
Special Thanks to Nitin M who made these all concepts very easy.

--

--

hemasai jammana

Software Engineer, learning and exploring new technologies.