Spring Tutorial - Spring IoC Container

 What is a Spring IoC container?

IoC container means Inversion of Control. IoC is a container in spring which takes care of instantiating, configure, and assemble of the object. XML file gives the input to IoC container and IoC container works accordingly.

IoC container is responsible for:

        1.     Instantiate the application class

        2.     Configures its object

        3.     Bind the dependencies between the objects.

 

How many IoC are present in Spring?

Spring supports two IoC container:

        1.     BeanFactory

        2.     ApplicationContext

These two classes are responsible to perform the function of IoC containers.


In this tutorial, we will be discussing Bean factory and ApplicationContext in depth.

        1.     BeanFactory

BeanFactory is the basic IoC Container. It gives the basic support to Dependency Injection. BeanFactory is defined by org.springframework.beans.factory.BeanFactory interface.

To provide the backward compatibility to third party applications BeanFactory includes BeanFactoryAware, InitializingBean, DisposableBean.

XMLBeanFactory is the implementation class for BeanFactory. XMLBeanFactory reads the configuration metadata from an XML file and creates the fully configured bean.

BeanFactory is used in lightweight applications like mobile applications or applet-based application.

Let us look at the implementation of BeanFactory using XMLBeanFactory class

We will be creating three files HelloWorld.java, MainApp.java and Beans.xml

 

HelloWorld.java

MainApp.java

Main points in MainApp.java

        1.     We have used XMLBeanFactory to create a factory object. This factory object will depend upon xml file (Beans.xml) which will be read using ClassPathResource.

        2.     Using the above factory object, we called getBean(“helloWorld”)  which will return an object that we should cast into required object type.

Beans.xml

Running and compiling the file will give the output:

Output

Your message: Hello World

        2.    ApplicationContext

This IoC container class is the advanced version of BeanFactory. BeanFactory is built on top of ApplicationContext. ApplicationContext provides BeanFactory features and its features as well like message resource binding, integration with AOP etc.

ApplicationContext is much heavier than BeanFactory.

Similar to BeanFactory, ApplicationContext also has implementation classes. ApplicationContext has three implementation classes which are given below. All these container loads the definition of the beans from the XML file.

        1.     FileSystemXmlApplicationContext – In this we need to provide the full path to XML bean configuration file to the constructor.

        2.     ClassPathXmlApplicationContext – In this no need to the provide full path to XML file. Classpath should be set properly because the container will look bean configuration XML file in the classpath.

        3.     WebXmlApplicationContext – the container will load all the beans from an XML file from within the application.

Let us look at an example of ApplicationContext using ClassPathXmlApplicationContext.

As done previously, we will create three files HelloWorld.java, MainApp.java and Beans.xml

HelloWorld.java

MainApp.java

Beans.xml

Running and compiling the file will give the output:

Output

Your message : Hello World


What is the difference between BeanFactory and ApplicationContext?

The difference between BeanFactory and ApplicationContext are as follows:

        1.     In BeanFactory the beans are loaded on demand. BeanFactory uses lazy loading. Whereas, In ApplicationContext the beans are loaded Eagerly.

        2.     BeanFactory is light Weight whereas ApplicationContext is heavyweight.

        3.     BeanFactory is the base implementation whereas ApplicationContext implements the BeanFactory.

        4.     ApplicationContext provides additional features like message resource binding and integration of spring with AOP which are not available with BeanFactory.

 

Conclusion

BeanFactory as well as ApplicationContext both are handy when building the spring framework it’s the matter of requirement when we should use BeanFactory and when ApplicationContext.

With this conclusion, we come to end of the chapter. Further, do refer to the previous chapter of this series.

As a next chapter, we will be discussing “Spring Tutorial – Bean Definition

Post a Comment

0 Comments