Spring Inversion Of Control

 

Spring Inversion Of Control


Hi everyone, let's  start with a simple example, do follow the blog to get an insight into IOC  (Inversion

 Of Control) in spring.


Step -1


  • First create a spring project and then create a package. 

  • In this example I use the spring project name as ioc. 

  • Create an interface called Sim.

  • This interface has two void methods. calling() and data().









 

 

 

 

Step -2


  • Now create a class called serviceProviderOne using the Sim interface.

  • This has override calling() and data() methods.







Step -3 


  • Create another class called ServiceProviderTwo using the Sim interface.

  • Also override calling() and data() methods.



 

 

 

 

Step -4


  • Create a  class named Mobile. This class  has a main method. 

  • You  can use methods in the ServiceProviderOne class and ServiceProviderTwo class as below. 

  • Create objects using these two classes and call these methods.







When you run the application  you will get the  following output.










If you want to use these methods in multiple classes and locations, you need to create Class objects in

 those classes and locations. Moreover, Spring creates objects for us and spring manages those

 objects. In IOC there is a config file that has classes we need to create objects. So spring has its own

 container called IOC. This container reads the config file and all the classes mentioned in that config

 file and creates objects for those classes. And it's going to manage that inside it’s container. 

 


If you want to use the class's methods, you can use the getBean() method for that. To use 

ServiceProviderOne class’s methods, you can use them using getBean(“ServiceProviderOne”)

 


There are two types of IOC containers. 

  1.  BeanFactory 

  2.  ApplicationContext


So let’s see  which one we should use and what is best for programming. As an example you have to 

choose one of these two items, a Pentium 3 computer or Corei7 laptop. Which one do you choose? 

You will definitely choose Corei7 laptop. Because it has all the features of a Pentium 3 computer and 

many extra features and performances.


BeanFactory is like a Pentium 3 computer and ApplicationContext is like a Corei7 laptop.  

Beanfactory and ApplicationContext are just interfaces. And they both are containers. So if you need

 to use an interface you need an implemented class. So an ApplicationContext implementation class 

is available called ClassPathXmlApplicationContext. This ClassPathXmlApplicationContext 

implements ApplicationContext


You should have your config file  in  case of beans.xml. Then use ApplicationContext and 

 ClassPathXmlApplicationContext to read that config file. your sample code should look like  this:

 


ApplicationContext context = new ClassPathXmlApplicationContext(“beans.xml”);


First you need to create a beans.xml file. In the beans.xml file you need to create <bean></bean> for 

an object. For example, you need to create an object for the ServiceProviderOne class like that.


<bean id = "serviceProviderOneRef"  class="com.springioc.tilanga.ioc.ServiceProviderOne"></bean>

    <bean id = "serviceProviderTwoRef"  class="com.springioc.tilanga.ioc.ServiceProviderTwo"></bean>


In this case com.springioc.tilanga.ioc.ServiceProviderOne is the fully qualified path of the class. So to 

track this class you need to use use id. Id is the reference of this class. Before using this one we need 

to configure xsd into a beans.xml file.


<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:schemaLocation="http://www.springframework.org/schema/beans

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



So, now you can use this beans.xml.








In the Mobile class the main method code changes as given 


ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

       ServiceProviderOne objOne = (ServiceProviderOne)context.getBean("serviceProviderOneRef");


       ServiceProviderTwo objTwo = (ServiceProviderTwo)context.getBean("serviceProviderTwoRef");


       objOne.calling();

       objOne.data();

       objTwo.calling();

       objTwo.data();


In this case (ServiceProviderOne) part is used to cast the object to the ServiceProviderOne class type.




After running this application you can get results like before.


Also you can do this using the Sim interface.


ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

       Sim objOne = (Sim)context.getBean("serviceProviderOneRef");


       Sim objTwo = (Sim)context.getBean("serviceProviderTwoRef");


       objOne.calling();

       objOne.data();

       objTwo.calling();

       objTwo.data();



You can do the same thing as below,


ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

       Sim objOne = context.getBean("serviceProviderOneRef", ServiceProviderOne.class);


       Sim objTwo = context.getBean("serviceProviderTwoRef", ServiceProviderTwo.class);


       objOne.calling();

       objOne.data();

       objTwo.calling();

       objTwo.data();


We can get the same output using this code as well.


Hope you got an insight to work on IOC. Keep following my blogs to learn more. Happy Coding !! 




Comments

Post a Comment