In this post, I'm going to discuss some more basic principles of Spring Framework.
DI (Dependency Injection) Basics
DI stands for Dependency Injection. DI is one of the form of IOC. Spring Framework was designed based on this pattern. As we discussed in my previous post, DI means “Resolving the bean dependencies”. In Spring Applications, Spring IOC Container resolves these dependencies automatically. NOTE:- IOC and Dependency Injection follow Push Model.
Advantages of DI
In this section, we will discuss about what are all the major advantages of DI. Because of DI follows Push Model, it has the following advantages:
Developer is not responsible for creating of objects
Reduces the application complexity
Reduces development time
Easy to do the application unit testing
Decreases components dependencies
NOTE:- What is the main responsibility of DI? Object Dependencies Management
Types of DI
DI (Dependency Injection) can be divided into the following three main categories:
We will discuss these categorizes one by one in detail with suitable examples in the coming sections.
Setter Injection (Setter DI)
As we know, DI means resolving bean dependencies. To resolve bean dependencies, we can use different techniques.
If we use setter method to resolve bean dependencies, then this kind of DI (Dependency Injection) is known as Setter Injection or Setter DI.
Setter DI Example:-
public class CardService
private CreditCard creditCard;
public void setCreditCard(CreditCard creditCard)
How to represent Setter DI in Spring Bean Configuration XML file?
By using <property> element Syntax:- For Primitive or String Properties:
Interface Injection (Interface DI) If we use interface to resolve bean dependencies, then it is known as Interface Injection or Interface DI. How to implement Interface Injection? Spring framework does NOT support Interface Injection. Why Spring framework does NOT support Interface Injection? Because we can NOT instantiate an Interface. That’s it all about “Spring Framework DI” concept. We will discuss some more important Spring Basics in my coming posts.
Please drop me a comment if you like my post or have any issues/suggestions. I love your valuable comments so much.