Pages Navigation Menu

Coding is much easier than you think

Spring Dependency Injection(DI)

 
spring logo
 
One of the most striking features of Spring, is the way loose coupling between the components has been achieved. The modules are made independent by keeping them abstract. The values / dependencies are injected to them through the underlying Spring framework’s IOC container.

Three types of dependency Injection are available.

1. Setter injection

2. Constructor injection

3. Interface injection

The value can be set using either constructor injection or setter injection.
 

Note : Interface injection is not mainly supported by spring.

 
The basic differences between these two types of dependency injection are :-  
Setter injectionConstructor injection
  1. In case a class has many attributes, partial injection is possible. i.e., values of only a few specific attributes can be set
 1. All the attributes that the constructor expects as parameter has to be injected.

 

 2. If a specific attribute is set using constructor as well as setter injection, then the setter injected value overrides the other one. 2. Constructor injected values cannot override setter injected values.

 


 
In the next tutorials to come, we will see in detail as to what exactly these two types of injection are and also try out hands on exercise about using these injection methods.