Spring – Dependency Injection

Spring – Dependency Injection

One of the main features of Inversion of Control (IoC) is Dependency Injection (DI). It is a process of supplying the dependencies from one object to others. The Spring-Core container module is responsible for injecting the dependencies. Dependency Injection is used to implement the Inversion of Control (IoC), or sometimes itself called Inversion of Control. The main motive of IoC is to keep all the Java classes independent from each other and provide automatic object creation. In Spring, Dependency Injection also ensures loose-coupling between the classes.

What is the need for Dependency Injection?

Let us understand this through an example. In an application, Class A needs the object of class B to call or operate a method such that, class A is dependent on class B. It might appear okay to depend on the other class, but in large scale applications, it will create a lot of problems. Hence, we need to avoid these types of dependencies.

Spring Inversion of Control (IoC) resolves such problems through Dependency Injection (DI). It makes the code more comfortable to use, learn, and test. Through DI, Loose-coupling can be possible between the classes by defining the interfaces.

Types of Dependency Injection

There are two types of dependency Injection present in Spring:

  1. Constructor Dependency Injection– In this DI type, the dependencies are injected with the help of a constructor. To set the dependency using Constructor, we need to add the dependency reference inside the <constructor-arg></constructor-arg> tag in the bean-Configuration file.

Following code shows how to add Constructor dependencies in the configuration file:

In the above code, myfortune is the reference to the constructor injection.

  • Setter Dependency Injection– In this DI type, the dependencies are injected with the help of a setter method. The Setter injection type is more accessible as compared to the Constructor injection. To set the dependency using setter, we need to add the dependency reference inside the <property></property>tag into bean-configuration file.

Following code shows how to add Setter dependencies in the configuration file:

Example of Constructor Dependency Injection

Let’s understand the Constructor Dependency Injection with the help of an example. In this example, the Fortune and Coach are the interfaces containing unimplemented methods. The GoodLuckFortune class implements the Fortune interface and its unimplemented methods. Similarly, the Cricket_Coach class implements the Coach interface and its unimplemented methods.

Following are the steps to create an example of Constructor Dependency Injection:

Fortune.java

GoodLuckFortune.java

Coach.java

Cricket_Coach.java

In the above Cricket_Coach class, we have created an arg-constructor for performing dependency injection.

applicationContext.xml

The above bean-configuration file (applicationContext), contains the bean information inside the <beans></beans> tag. The <beans>tag is used to define multiple beans. Inside the beans tag, <bean></bean> tag is defined for defining different beans. The <bean>tag is used to define a single bean class.

It also contains the constructor reference for dependency injection inside the <constructor-arg></constructor-arg>tag.

App.java

The above App class contains the main() method.

OUTPUT

App class contains the main() method

Now, we are going to create an example of Setter Dependency Injection.

Example of Setter Dependency Injection

We have discussed above the example of Constructor dependency injection. Now, we will create the SDI example. It is similar to the above example. Fortune and Coach are the interfaces with the unimplemented methods. The GoodLuckFortune and Cricket_Coach are the Bean classes which implement the Fortune and Coach interface, respectively.

Following are the steps to create an example of Setter dependency injection:

Fortune.java

GoodLuckFortune.java

Coach.java

Cricket_Coach.java

In the above Cricket_Coach class, we have used the setter method for performing the dependency injection.

applicationContext.xml

In the above xml file, we have defined the dependency reference inside the <property></property> tag.

App.java

OUTPUT

we have defined the dependency reference inside the property