We have already gone through the concept of spring IoC in Spring Tutorial - Spring IoC Container. The main purpose of Spring IoC is to manage the spring bean object.
In this chapter,
we will see how we can resolve the dependencies of the objects and make them
loosely coupled.
This chapter will
include:
1.
What
is Dependency Injection (DI) in spring?
2.
Why
we use Dependency Injection (DI) in spring?
3.
How
does Dependency Injection (DI) work in spring?
4.
What
are the Different Types of Dependency Injection (DI) in spring?
5.
Difference
between Setter Injection and Constructor injection?
6.
Which
Dependency Injection (DI) is best in spring?
If all these
topics are covered and understood, then at the end of this chapter Dependency
Injection (DI) in spring will become easy. So, let us get started.
What is Dependency Injection (DI) in spring?
Dependency
Injection (DI) in spring is a design pattern which eliminated the dependency
from the programming code.
Dependency
Injection (DI) is an approach using which an object receives the dependency in
the form of another object which it depends upon from outside the class.
By inserting the
dependency from outside the class we reduce the coupling of the objects.
Why we use Dependency Injection (DI) in Spring?
To understand when
we should use Dependency Injection (DI) let us first understand what will
happen if we are not using Dependency Injection (DI).
Consider the following example where Employee.java is dependent on the Address object.
In the above example,
whenever we create the Employee object the address will be set to “India”. Like
this, the Employee class is tightly coupled with the address that is passed in
the constructor. Hence, in the above example, both emp1 and emp2
will have an address set to “India”
To resolve this
tight coupling of the object, spring has introduced the concept of Dependency
Injection (DI).
Let us implement
the same example using Dependency Injection (DI) mechanism.
In the above example, we can see that for address we are inserting the dependency from outside rather than inserting within the class. Hence, for emp1 and emp2 the value of address will be different.
How does Dependency Injection (DI) work in spring?
Dependency Injection (DI) is a design pattern which is the implementation
of IoC. Dependency Injection (DI) on the mechanism of adding the dependency from
outside. In the above example, we can see that we have inserted the dependency
from outside.
There are multiple ways by which we can implement Dependency Injection (DI). Dependency Injection (DI) will result in the loose coupling between the classes.
What are the Different Types of Dependency Injection
(DI) in spring?
There are mainly two
ways by which we can implement Dependency Injection (DI).
1.
Constructor Based Dependency Injection (DI).
2.
Setter
Based Dependency Injection (DI).
Let us discuss both these types with example.
Constructor Based Dependency Injection (DI)
Constructor Based Dependency
Injection (DI) can be achieved by inserting the dependencies using a constructor.
The dependencies are injected using the constructor.
Let us take the example of Employee class where dependencies id and name
are inserted using Constructor.
Compiling and running the file will give the below output:
Output:
100 0
0 Australia
101 India.
Setter Based Dependency Injection (DI)
The setter based Dependency Injection (DI) can be achieved by inserting the
dependencies using setter methods. The dependencies are injected using setter
methods of the class.
Let us take the example of the same Employee class where dependencies id
and name are inserted using setter methods.
In the above example, we have used the setAddress method to insert the dependency.
Difference between Setter Injection and Constructor injection?
The main differences between setter injection and constructor injection
are as follows:
1.
Partial injection – with setter injection the partial
injection of dependencies is possible whereas with constructor injection there is
no partial injection. All the fields of the object will be injected with dependencies.
2.
Overriding – setter based injection will
override the constructor based injection. Hence more priority will be given to
setter based injection rather than constructor based injection.
3.
Changes in dependencies – with setter based injection
we can change the dependencies without changing the whole object whereas with
constructor based injection the whole object will be created. Hence, setter based
injection will provide more flexibility to the application.
Which Dependency
Injection (DI) is best in spring?
Usually, constructor based injection is more suitable as it ensures that
all the required properties are set. With setter injection, we cannot guarantee
that the required properties are set.
One more case is if we are going to use constructor injection then whenever
we will inject the dependencies using constructor the object will get created
this is not in the case of setter injection.
Another scenario is when we have a lesser number of properties then we
should go for setter injection as it will be more convenient. With a larger
number of properties, we should write constructor injection as it will inject
the dependencies at once.
That is all for this topic. Hope you are comfortable with the language
and the way of explanation.
The next topic in the series will be the Spring tutorial - Java Based
Configuration
0 Comments