Inversion of Control

Inversion of Control

Inversion of Control (Ioc) in Spring is a mechanism to achieve loose coupling. To achieve loose dependencies and dynamic binding of objects at runtime, dependencies of objects are injucted by other assembler object.

Consider the following scenario where we have a interface Animal with a method color and two classes Dog and Cat for providing its implementation.
One main class to run it.

Public Class Animal {
Public void color(){ };
}
Public Class Dog implements Animal{
   Public void color(){
    System.out.println(“White”);
}
}
Public Class cat implements Animal{
   Public void color(){
    System.out.println(“Black ”);
}
}
Class main{
 Public static void main(String args[])
{
    Animal an= new Dog();
    An.color();
}
}

Now, we can see that as the object belong to Dog Class type so the method inside Dog class will run and Output will be “white”.

Now, here Dog class object has the control to call the method and if we want that this control will be in users hand or it can be decided at runtime that whose method will be called then we have to invert the control. This is nothing but Ioc(Inversion of Control).

In other way if we want that it should be decided at run time that whose object will be created at runtime according to the need of the user. By doing it in above mentioned manner we cannot do that therefore we use loose coupling. The above explained example is an example of tight coupling.

Inversion of Control (Ioc) is achieved by Dependency Injuction (DI).In Denpendency Injuction (DI) we injuct the dependency of a object into another object to make it ready for our use. The program that injucts dependency into an object is called as IoC container.

An IoC Container is responsible for:

• Object instantiation.
• Configure the object.
• Assemble the object.

IoC container gets the information from XML file and Works Accordingly. Basically,

There are two types of IoC containers:

  1. BeanFactory interface
  2. ApplicationContext interface

BeanFactory interface:

It is inside “org.springframework.bean” package and its implementation is provided by XMLBeanFactory (class).
The constructor of XMLBeanFactory Class takes String as input so we can pass the path of XML file into it.

Resource resource=new ClassPathResource(“ApplicationContext.xml”);
BeanFactory=new XMLBeanFactory(resource);

Resource is a interface which represents external resources and ClassPathResource is a class which provides its implementation.

ApplicationContext interface:

It is inside “org.springframework.context” package and ClassXMLPath class provides its implementation.

The constructor of ClassXMLPath takes String path as argument. So, we can pass the Path of XML file here.

ApplicationContext context = new ClassPathXML(“file.xml”);

Difference between using ApplicationContext and BeanFactory interface:

ApplicationContext is built on the top of Bean Factory interface. It adds some extra functionality than BeanFactory such as simple integration with spring AOP, Message resource handling, ability to resolve textual messages from a properties file etc. So, it is better to use ApplicationContext over BeanFactory. Beanfactory is still useful for lightweight applications.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

code