Spring Core

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.


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


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.

Share this article on

Comment (11)

  1. Karibasappa G C (KB)

    Thanks Shailendra !!

    I have already done this for Spring MVC and Spring Security related posts where i have given an option to download the entire project.

    But for core concepts , i have not done this, I will do it for sure, Please give sometime.

    Thanks for alarming on this!!

  2. shailendra tiwari

    Sir your website is very nice but some problem
    You are not mension jar files ig we run your app how to know which jar files are support yor app
    pls sir mention jar files i’m waiting……………….

  3. HI, karibasappagc.
    I agree with basavaraj.
    very clear and informative tutorial. I enjoy reading it last 2 days 🙂
    Great Thanks!

  4. i did not seen any tutorial like this, i really thank full for sharing your valuable knowledge in spring with good and valid examples.

    i have plan to learn spring long back, i search many tutorials there i find only theory kind of stuffs, i find your java guide this is very useful and now i feel happy. i proudly say i learned spring.

    thank you karibasappa sir

Leave a Comment

  • You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code lang=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" extra="">

    • Please answer this simple challenge to post your valuable comment *