Dependency Injection(DI) & Inversion of Control(IoC) In Java Spring

Tusharchavhan
5 min readJun 13, 2021

In this, we see What is Dependency Injection(DI), Types of DI, what is Inversion of Control(IoC) in the Spring framework.

What is Dependency Injection?

Basically, dependency means one thing is dependent on another thing, This also happens in programming in which one class depends on another class.

Let’s say we have a Student class that has fields Id, Name, Email, Address. The Address is a class and the Student class is totally dependents on the Address. So we required the object of Address in Student. The spring automatically injects the Address object in the Student class that concept is called Dependency Injection. The spring provides loosely coupled using dependency injection.

Types of Spring Dependency Injection:-

  1. Constructor Dependency Injection(CDI):-

In this, the DI will be injected with the help of the Constructor. Now to set the DI as CDI in bean, it is done through the bean-configuration file(.XML) For this, the property to be set with the CDI is declared under the <constructor-arg> tag in the bean-config.

Employee.java

It contains three properties id, name, address(dependent object), two constructors, show() method to show the records of the current object

Fig:-Employee.java

Address.java

This class contains three properties, one constructor and toString() method to return the values of these object

Fig:-Address.java

Bean configuration file(applicationContext.xml)

In this, The ref attribute is used to define the reference of another object

Fig:- applicationContext.xml

2. By using Setter methods(SDI):-

This is the simpler of the two DI methods. In this, the DI will be injected with the help of setter and/or getter methods. Now to set the DI as SDI in the bean, it is done through the bean-configuration file For this, the property to be set with the SDI is declared under the <property> tag in the bean-config file.

Address.java

This class contains four properties, setters, getters, toString() method.

Fig:-Address.java

Employee.java

It contains three properties id, name, address(dependent object), setters, getters with show() method.

Fig:-Employee.java

Bean configuration file(applicationContext.xml)

The ref attribute of property elements is used to define the reference of another bean.

Fig:-applicationContext.xml

Advantages of Dependency Injection

  1. DI allows a client the flexibility of being configurable. Only the client’s behavior is fixed.
  2. Testing can be performed using mock objects.
  3. Loosely coupled architecture.
  4. DI does not require any changes in code behavior it can be applied to legacy code as refactoring.
  5. DI allows a client to remove all knowledge of a concrete implementation that needs to use. It is more reusable, more testable, more readable code.
  6. DI makes it possible to eliminate, or at least reduce unnecessary dependencies.
  7. DI allows concurrent or independent development.
  8. DI decreases coupling between a class and its dependency.

Disadvantages of Dependency Injection

  1. DI creates clients that demand configures details supplied by construction code.
  2. DI can make code difficult to trace because it separates behavior from construction; this means developers refer to more files to follow how a system performs.
  3. DI can cause an explosion of types, especially in languages that have explicit interface types like C# and Java.
  4. DI can encourage dependence on the DI framework.

What is Inversion of Control?

It means giving them control of creating and instantiating the spring beans to the Spring IOC container and the only work the developer does is configuring the beans in the spring XML file.

The main tasks performed by the IoC container are:

  • to instantiate the application class
  • to configure the object
  • to assemble the dependencies between the objects

Types of IoC containers:-

  1. BeanFactory

This is the simplest container providing the basic support for DI and defined by the org.springframework.beans.factory. There are a number of implementations of the BeanFactory interface. The most commonly used BeanFactory implementation is the XmlBeanFactory class. This container reads the configuration metadata from an XML file and uses it to create a fully configured system or application.

The BeanFactory is usually preferred where the resources are limited like mobile devices or applet-based applications

2. ApplicationContext

The Application Context is Spring’s advanced container. Similar to BeanFactory, it can load bean definitions, wire beans together, and dispense beans upon request. This container is defined by org.springframework.context.ApplicationContext interface.

The ApplicationContext includes all functionality of the BeanFactory, It is generally recommended over BeanFactory. BeanFactory can still be used for lightweight applications like mobile devices or applet-based applications.

The most used ApplicationContext implementation is ClassPathXmlApplicationContext − This container loads the definitions of the beans from an XML file. Here you do not need to provide the full path of the XML file but you need to set CLASSPATH properly because this container will look like a bean configuration XML file in CLASSPATH.

--

--