Thursday, 4 January 2018

What is IOC Container in Spring? Difference between BeanFactory and ApplicationContext


       The Spring IOC Container is a core of the Spring Framework, It will create the objects, wire them together, configure and manage complete life cycle from creation till destruction.
       The Spring Container uses DI(Dependency Injection) to manage the components that make up an application.

The Container will create the objects, those objects are called as Spring Beans.

       Spring Bean is nothing special, any object in the Spring framework that we initialize through Spring container is called Spring Bean. Any normal Java POJO class can be a Spring Bean if it’s configured to be initialized via container by providing configuration metadata information.

       The container gets its instructions on what objects to instantiate, configure and assemble by reading the configuration metadata provided.  The configuration metadata can be represented either by XML, Java annotations or Java code. The following diagram represents a high-level view of how Spring Container works.



There are two types of Spring IOC Containers,

  •  BeanFactory Container
  •  ApplicationContext Container


BeanFactory Container:

             BeanFactory is the root interface of Spring IoC container.  It is defined in the org.springframework.beans.factory package. It is the actual representation of the Spring IoC container responsible for containing and managing the Spring beans. The BeanFactory, sources application object, configures them and takes care of assembling the dependencies between objects. One of the most popularly used implementation of BeanFactory is the XMLBeanFactory. The XMLBeanFactory allows the representation of objects and their rich dependencies in terms of XML. The XmlBeanFactory takes XML configuration metadata to create the fully  configured application.

Following is the example of simple hello world application using  XMLBeanFactory.

Beans.xml:-

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<bean id="helloWorld" class="com.src.sample.HelloWorld"> 
    <property name="message" value="Hello World!"/> 
</bean>

       The above XML code shows the contents of the bean xml configuration.  It has a single bean configured that has a single property by the name message.  A default value is set for the property.

Next, HelloWorld.java


 package com.src.sample; 
 public class HelloWorld { 
 
      private String message; 
      
      public void setMessage(String message){ 
           this.message = message; 
      } 
      public void getMessage(){ 
           System.out.println(message);
      } 
 }

The main class i.e HelloWorldMain.java to call the bean.


package com.src.sample; 
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.xml.XmlBeanFactory; 
import org.springframework.core.io.ClassPathResource; 

public class HelloWorldMain {     
     public static void main(String[] args) {           
           XmlBeanFactory factory = new XmlBeanFactory (new ClassPathResource("Beans.xml")); 
           HelloWorld obj = (HelloWorld) factory.getBean("helloWorld"); 
           obj.getMessage(); 
     } 
}

        In the above program, the XmlBeanFactory loads the metadata XML based on the CLASSPATH variable.  It uses the configuration metadata to load, assemble and dispense the beans upon request.
Finally, a method on the bean (getMessage()) is invoked to get the desired result.

       The BeanFactory is preferred when there are limited resources like mobile devices or applets.
BeanFactory is a subset of ApplicaitonContext and provides lesser functionalities. When we need full capabilities with respect to configuration handling then we go for ApplicationContext.



ApplicationContext Container:--

        ApplicationContext is defined by the org.springframework.context.ApplicationContext interface. This is similar to BeanFactory, in that it can load bean definitions, wire them together and dispense beans upon request. In addition, ApplicationContext can also perform more enterprise functionalities like transaction, AOP, resolving text messages from properties files, and push application events to interested listeners.

    The most popularly used ApplicationContext implementations are:

    • FileSystemXmlApplicationContext       
            This implementation loads the definitions of the beans from an XML file. It is required to provide the full path of the XML bean configuration file to the constructor.


    • ClassPathXmlApplicationContext
          This container loads the definitions of the beans from an XML file.  However, it is not required to provide the full path of the XML file. It does require you to set CLASSPATH properly because this container will look for bean configuration XML file in the specified CLASSPATH.

    • XmlWebApplicationContext
    This container loads the XML file with definitions of all beans from within a web application.


    Following is the one sample example of FileSystemXmlApplicationContext,

    HelloWorldMain.java,

    package com.src.sample; 
    import org.springframework.context.ApplicationContext; 
    import org.springframework.context.support.FileSystemXmlApplicationContext;
    
    
    public class HelloWorldMain {   
         public static void main(String[] args) { 
               ApplicationContext  context = new FileSystemXmlApplicationContext ("C:/Program/workspace/SpringExample/src/Beans.xml"); 
               HelloWorld obj = (HelloWorld) context.getBean("helloWorld"); 
               obj.getMessage(); 
         } 
    }
    


    Difference between BeanFactory and the ApplicationContext:--

            The org.springframework.beans.factory.BeanFactory and the
     org.springframework.context.ApplicationContext interfaces acts as the IoC container. The ApplicationContext interface is built on top of the BeanFactory interface.

            It adds some extra functionality than BeanFactory such as simple integration with Spring’s AOP, message resource handling (for I18N), event propagation, application layer specific context (e.g. WebApplicationContext) for web application. So it is better to use ApplicationContext than BeanFactory.

    No comments:

    Post a Comment