Spring Constructor Injection is a form of Dependency Injection where the objects are injected into the client using the client constructor.
Example of Spring Constructor injection
Github – Source Code for this example
In typical software development, classes collaborate with each other to achieve the desired functionality. e.g. In an ATM (Automated Teller Machine) system, the ATM class and Printer class can collaborate with each other to print the balance information for an account.
Collaboration between classes is usually expressed as dependency where the reference of one class is held by another class. e.g. The ATM class holds a reference to Printer class (see line 5).
The dependencies need to be ‘resolved’ before the desired functionality can be achieved. e.g. By ‘resolved’ we mean that an instance of Printer class needs to be created and associated with the ‘printer’ member in ATM class. When dependency resolution is not performed by the class itself but is left to be done by an external agent (e.g. Spring Framework) it is called dependency injection. So in our example, Spring will create an instance of the Printer class and associate the instance with the ‘printer’ member in ATM class. However as the ‘printer’ member in ATM class is private, the ATM class needs to expose its dependency to Spring for it to inject the Printer instance into the ATM class. e.g. If the ATM class exposes its dependency on Printer class as a constructor so that Spring can inject it then this is called as Constructor injection (see line 7).
Now, Spring can create an instance of Printer class and inject it via the constructor of ATM class via constructor injection as we will see in the sample program below.
Spring Constructor Injection Sample Program
Required Maven Dependencies for Spring Constructor Injection
Source Code For Spring Construction Injection
Create a Printer class with a method printBalanceInformation()
Create the ATM class with a dependency on Printer class. The ATM class delegates the call to print the balance information to the Printer class (see line 12).
Now, before Spring framework can perform constructor injection, it needs to know about the ATM and Printer classes. This is achieved by declaring the ATM and Printer classes as Spring beans in spring-config.xml (see line 10 and line 14). Note that constructor injection is achieved by passing a reference of the ‘printer’ class to ATM class bean definition(see line 11)
Finally, we need to test our constructor dependency injection setup. We need to tell Spring framework to use the ‘spring-config.xml’ to load our beans(see line 11). We get the reference to ATM class through Spring using the name ‘atm’ (see line 12). In this step, Spring will resolve the dependency on Printer class by injecting it using constructor injection. We call the printBalanceInformation() on ATM class (see line 14) with some accountNumber (see line 13).
Here’s the spring config file for the example.
In the next tutorial, we will look at the next form of dependency injection, which is setter injection.