In this article, we will initially see the meaning of Dependency Injection (DI), and then get to know the different types of bean injection in Spring.
What is Dependency Injection?
Dependency Injection (DI) is a pattern that implements Inversion of Control (IoC), where the control being inverted is the setting of object’s dependencies. It is a process intended to increase the independence of Java classes, which results with the possibility to reuse these classes and test them independently from other classes.
Java classes, or beans represent the objects that form the foundation of an application. Beans are instantiated, assembled and otherwise managed by a container or a framework.
Although it may sound as a bit complex, dependency injection can actually make your code significantly simpler, easier to understand, and easier to test. Dependency Injection, which can also be called as wiring helps in gluing these seemingly unrelated classes together and independent at the same time.
Dependency Injection in Spring
Among many other modules, Spring Framework implements the Inversion of Control (IoC) principle by which the control of objects is transferred to a container or a framework.
An application context inside a Spring application loads bean definitions and wires them together.
Dependency Injection in Spring can be done through setters and constructors.
Constructor-Based Dependency Injection
When we use constructor-based DI, the container will invoke a constructor with a number of arguments, each representing the dependencies we want to set. Each argument is distinguished primarily by its type, then by the name of the attribute and index. Let’s see this with a short example:
@Configuration
public<span style="font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif;"> </span>
class
AppConfig {
@Bean
public
Car car() {
return
new
CarImpl();
}
@Bean
public
Wheel wheel() {
return
new
Wheel(car());
}
}
Setter-Based Dependency Injection
The setter-based DI, or setter-based wiring is accomplished by the call of setter methods of your beans by the container, after invoking a no-argument static factory method to instantiate your bean. We can see this illustrated in the following example using annotations:
@Bean public Wheel wheel() { Wheel wheel = new Wheel(); wheel.setCar(car()); return when; }
Which one should you use?
You can use either type of DI that best suits your needs in the project that you are working on. It is worth mentioning, however, that setter-based DI is used more frequently because of its simplicity in comparison to the constructor-based one.
Where Can You Learn More?
This article has briefly touched the concepts of dependency injection, and the different types of bean injection in Spring.
You can learn more about this topic by reading another more detailed article published in Baeldung.
You can also read Martin Flower’s explanations:
You can also get a much in depth reference to Spring implementations of IoC and DI in the Spring Framework Reference Documentation.
There is also a good book that covers DI in fine detail by Dhanji R. Prasanna.