Spring - Inversion of Control (IoC)

Spring - Inversion of Control (IoC)

The Inversion of Control (IoC) is a process where the objects define their dependencies, that is, the dependencies of other objects with they are working. It is done simply by an argument-constructor, argument to a factory method, or by setting properties of the objects when they are being constructed. The dependencies are injected by the container when the bean is created. The Inversion of Control (IoC) can be achieved through various techniques, such as Dependency Injection (DI), Service Locator Pattern (SLP), and Strategy Design Pattern (SDP).

The Inversion of Control (IoC) can also be defined as the design process of externalizing the construction and management of the objects. It is an approach to outsourcing the creation and management of the objects. The object factory handles outsourcing.

We can also define the Inversion as a reversal of the legacy. The legacy is to declare and initialize the variables through the instances of the objects we create.

There are two types of IoC containers:

  • BeanFactory - The BeanFactory interface is available in the org.springframework.beans package. It is the root interface for accessing the Spring bean container. It also provides basic configuration techniques to manage the bean objects of any type. We can say that BeanFactory is a legacy method to use.
  • ApplicationContext –The ApplicationContext interface is available in the org.springframework.context package. It is a complete superset of a BeanFactory interface. It is also known as a central interface that provides the configuration to an application.

The following code shows the instantiation of the BeanFactory interface:

The XmlBeanFactory is the implementation class used to initialize the BeanFactory interface.

Resource resource = new FileSystemResource("beanfactory.xml");
XmlBeanFactory fact = new XmlBeanFactory(resource); 

OR

ClassPathResource resource = new ClassPathResource("beanfactory.xml");
XmlBeanFactory fact = new XmlBeanFactory(resource); 

The following code shows the instantiation of the ApplicationContext interface:

The ClassPathXmlApplicationContext is the implementation class used to initialize the ApplicationContext interface.

ClassPathXmlApplicationContext  applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

OR

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

Difference between ApplicationContext and BeanFactory

As we have discussed above that both the ApplicationContext and BeanFactory are the Java interfaces and, both are the types of IoC container. But the main difference between both the interfaces is, the BeanFactory provides the basic IoC and DI features whereas, the ApplicationContext delivers the advanced features.

Some of Other differences are given below:

  • BeanFactory interface does not support internationalization (i18n), whereas ApplicationContext provides support for it. Internationalization is the process of constructing an application in such a way that any languages can adapt it without any modifications.
  • BeanFactory interface uses lazy initialization technique, whereas ApplicationContext uses eager initialization, i.e., BeanFactory creates only a singleton bean when requested, but ApplicationContext produces all singleton beans at the time of initialization.
  • BeanFactory does not support annotation-based configuration, whereas ApplicationContext supports annotations such as @Autowired, @ComponentScan, etc.

Example of IoC (Inversion of Control)

Now, we are going to create an example of Inversion of Control (IoC). In this example, we are going to create the following Classes and Interfaces:

  1. Sim Interface
  2. Jio Class
  3. Airtel Class
  4. applicationContext XML File
  5. AppTest Class

The first step is to add Spring Framework dependencies in pom.xml. To develop any Spring application, we need to add the spring-web MVC dependency.

Following is the dependency required by the application :

 
 org.springframework
 spring-webmvc
 5.1.6.RELEASE
  

The Sim Interface contains three unimplemented methods call(), message(), and dataUsage().

Sim.java

public interface Sim {
             public void call();
             public void message();
             public void dataUsage();  
  } 

The Jio.java is a POJO/ Bean class which implements the Sim interface and overrides its unimplemented methods.

Jio.java

public class Jio implements Sim {
             public void call() {
                         System.out.println("calling through jio");
             }
             public void message() {
                         System.out.println("messaging through jio");
             }
             public void dataUsage() {
                         System.out.println("dataUsage through Jio"); 
             }
   } 

The Airtel.java is a POJO/ Bean class which implements the Sim interface and overrides its unimplemented methods. It is similar to the Jio class.

Airtel.java

public class Airtel implements Sim {
             public void call() {
                         System.out.println("calling through airtel");
             }
             public void message() {
                         System.out.println("messaging through airtel");
             }
             public void dataUsage() { 
                         System.out.println("dataUsage through airtel");
             }
  } 

The applicationContext.xml is a Spring IoC Container which contains the bean objects. In this file, we have added the entry of the Jio class as a bean. All the beans are defined inside the <bean>…</bean> tag.

applicationContext.xml

 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 
 
  

The AppTest.java is the main class which contains the main() method. In this class, we have instantiated the ApplicationContextContainer through its implementation class ClassPathXmlApplicationContext.

AppTest.java

import org.springframework.context.ApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 public class AppTest 
 {
             public static void main(String[] args) {
           //create a Spring container 
                     ClassPathXmlApplicationContext applicationContext = new    ClassPathXmlApplicationContext("applicationContext.xml"); 
             System.out.println("config is loaded");
            //retrieving bean from the Spring container
              Sim sim = applicationContext.getBean("simID", Sim.class);
             sim.call();
             sim.message();
             sim.dataUsage();
             }
 } 

The following code shows that the application is configurable. We can easily change the Sim from Airtel to Jio.

ApplicationContext applicationContext = new    ClassPathXmlApplicationContext("applicationContext.xml");

To change the Sim, we only need to change the bean in the applicationContext file.

OUTPUT

applicationContext