IOC Container

Spring framework comes up with its own container whose job is to manage the beans life cycle, associate the dependent beans together (which is called wiring).

The container in spring is provided in the form of interfaces namely

1) BeanFactory

2) ApplicationContext

These 2 interfaces provided by spring acts as Containers, we can use any one of these containers based on the requirement.

Spring container uses the XML configuration to create the bean and wire among them.
Once the container loads the XML configuration, it will instantiate the beans and wire among the dependent beans and makes it ready for the application classes to use it.

For every bean defined in the XML configuration, there should be a corresponding java class.

So as shown below, Container reads the XML configuration and Java classes, then it instantiate and wires it if required and provide the readily available object to the application.

IOC_Container

Using Bean Factory container

Since BeanFactory is an interface, we should use its implementation class to use the container features.
XmlBeanFactory is the implementation class for BeanFactory and it can be used as below

  1. Resource resource=new ClassPathResource("beans.xml");  
  2. BeanFactory factory=new XmlBeanFactory(resource);
Resource resource=new ClassPathResource("beans.xml");  
BeanFactory factory=new XmlBeanFactory(resource);

We need to pass Resource object to XmlBeanFactory’s constructor,so we have created the ClassPathResource object first by passing the spring configuration file name and then passed it to the XmlBeanFactory’s constructor.

Now Spring container reads the beans.xml file and corresponding java classes and instantiate the beans and manages its entire life cycle.

Using ApplicationContext container

Since ApplicationContext is an interface, we should use its implementation class to use the container features.
ClassPathXmlApplicationContext is the implementation class for ApplicationContext and it can be used as below

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

We need to pass the string value to the ClassPathXmlApplicationContext constructor and it should be the spring configuration file name, in this case it is beans.xml.

Now Spring container reads the beans.xml file and corresponding java classes and instantiate the beans and manage its entire life cycle.

So both the containers are performing the same thing i.e. managing the beans life cycle.

Then what we should use ?

BeanFactory vs ApplicationContext

org.springframework.beans.factory.BeanFactory and the org.springframework.context.ApplicationContext
Both are spring containers and both are configured using XML configuration, both will perform the basic container functionality.

However ApplicationContext interface extends BeanFactory interface which means ApplicationContext will have some additional features along with all the features provided by BeanFactory.

Those additional features are as below

Internationalization

BeanFactory does not support internationalization (i18N) but ApplicationContext support it.

Event Publishing

ApplicationContext provides the facility to publish the events for the beans that are registered as listeners.

Spring AOP support

It provides integration with spring AOP

Since ApplicationContext has many advanced features along with all the features provided by BeanFactory, it is advisable to use ApplicationContext than BeanFactory.

BeanFactory is good to use if we don’t want to use any of the additional features provided by ApplicationContext.

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on