Thursday, November 23, 2017

What is difference between BeanFactory and ApplicationContext in Spring framework

BeanFactory vs ApplicationContext
The difference between BeanFactory and ApplicationContext in Spring framework is another frequently asked Spring interview question mostly asked Java programmers with 2 to 4 years experience in Java and Spring. Both BeanFactory and ApplicationContext provides a way to get a bean from Spring IOC container by calling getBean("bean name"), but there is some difference in there working and features provided by them. One difference between the bean factory and application context is that the former only instantiate bean when you call getBean() method while ApplicationContext instantiates Singleton bean when the container is started,  It doesn't wait for getBean to be called. 

This interview questions are third on my list of frequently asked spring questions e.g. Setter vs Constructor Injection and  What is the default scope of Spring bean. If you are preparing for Java interview and expecting some Spring framework question, It’s worth preparing those questions. 

If you are new in Spring framework and exploring Spring API and classes than you would like to check my post on some Spring utility functions e.g. calculating time difference with StopWatch and escaping XML special characters using Spring HtmlUtils. Coming back to BeanFactory vs ApplicationContext, let’s see some more differences between them in the next section.

By the way, if you are new to Spring framework then I also suggest you join a comprehensive and up-to-date course to learn Spring in depth. If you need recommendations, I highly suggest you take a look at Spring Framework 5: Beginner to Guru, one of the comprehensive and hands-on course to learn modern Spring. It' also most up-to-date and covers Spring 5.

BeanFactory vs ApplicationContext in Spring

Before seeing the difference between ApplicationContext and BeanFactory, let see some similarity between both of them. Spring provides two kinds of IOC containers, one is BeanFactory, and the other is ApplicationContext. Syntactically BeanFactory and ApplicationContext both are Java interfaces and ApplicationContext extends BeanFactory. 

Both of them are configuration using XML configuration file. In short, BeanFactory provides basic IOC and DI features while ApplicationContext provides advanced features. Apart from these, Here are a few more differences between BeanFactory and ApplicationContext which is mostly based upon features supported by them.

1) BeanFactory doesn't provide support for internationalization i.e. i18n but ApplicationContext provides support for it.

2) Another difference between BeanFactory vs ApplicationContext is the ability to publish events to beans that are registered as listeners.

3) One of the popular implementations of the BeanFactory interface is XMLBeanFactory while one of the popular implementations of the ApplicationContext interface is ClassPathXmlApplicationContext. On Java web application we use WebApplicationContext  which extends the ApplicationContext interface and adds the getServletContext method.

4) If you are using auto wiring and using BeanFactory than you need to register AutoWiredBeanPostProcessor using API which you can configure in XML if you are using  ApplicationContext. 

In summary, BeanFactory is OK for testing and non-production use but ApplicationContext is more feature-rich container implementation and should be favored over BeanFactory

Difference between BeanFactory and ApplicationContext in Spring frameworkThese were some worth noting difference between BeanFactory and ApplicationContext in Spring framework. In most practical cases you will be using ApplicationContext but knowing about BeanFactory is important to understand the fundamental concept of the spring framework. 

I mostly use XML configuration file and ClassPathXmlApplicationContext to quickly run any Spring-based Java program from Eclipse  by using the following snippet of code :

public static void main(String args[]){
    ApplicationContext ctx =
new ClassPathXmlApplicationContext("beans.xml");
    Hello hello =
(Hello) ctx.getBean("hello");

here beans.xml is your spring configuration file and “hello” is a bean defined in that spring configuration file. Here we have used ClassPathXmlApplicationContext  which is an implementation of ApplicationContext interface in Spring.

Further Reading
Spring Framework 5: Beginner to Guru
Spring Master Class - Beginner to Expert
Introduction to Spring MVC 4 By Bryan Hansen
Spring in Action 4th edition by Craig Walls

Other Spring questions and articles in Java

P. S. - If you are an experienced Java/JEE Program and want to learn Spring Security end-to-end, I recommend the Learn Spring Security course by Eugen Paraschiv, The definitive guide to secure your Java application. It's useful for both junior and experienced Java Web developers.


Anonymous said...

I have never really faced any situation where I have used BeanFactory, I mostly use ApplicationContext that too ClassPathXmlApplicationContext and its more than sufficient for almost all core Java programs. In web application you can use ClassPathXmlApplicationContext, forget about BeanFactory, they are just there due to legacy.


Bean Factory :-
Bean instantiation/wiring

Application Context :-
Bean instantiation/wiring
Automatic BeanPostProcessor registration
Automatic BeanFactoryPostProcessor registration
Convenient MessageSource access (for i18n)
ApplicationEvent publication

So if you need any of the points presented on the Application Context side, you should use ApplicationContext.

Javin @ data structure interview question said...

@Saral, Great summary. I use ApplicationContext by default, but from interview point of view, it's good to remember these differences.

Unknown said...

Hi, do you have any guide when to use which XmlApplicationContext?

I'm new to Spring and I have no idea when to use GenericXmlApplicationContext, ClassPathXmlApplicationContext, etc.


javin paul said...

@Ariady, you need ClassPathXmlApplicationContext because mostly you keep your application context file in Classpath and if you use this context then Spring will look for that file in classpath only.

Unknown said...

The only feature of the BeanFactory is really the Bean instantiation/wiring while the application context can be used for Automatic BeanPostProcessor registration, Automatic BeanFactoryPostProcessor registration, Convenient MessageSource access (for i18n), ApplicationEvent publication as well!

David Mayer @

Post a Comment