Saturday, March 18, 2023

Difference between BeanFactory and ApplicationContext in Spring Framework

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 provide a way to get a bean from the Spring IOC container by calling getBean("bean name"), but there is some difference in their working and features provided by them. One difference between the bean factory and application context is that the former only instantiates bean when you call getBean() method while ApplicationContext instantiates Singleton bean when the container is started,  It doesn't wait for the getBean to be called. 

These 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 a Java interview and expecting some Spring framework questions, It’s worth preparing those questions. 

By the way, if you are new to the 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: Beginner to Guru, one of the comprehensive and hands-on courses to learn modern Spring. It' also the most up-to-date and covers Spring 5.

Coming back to BeanFactory vs ApplicationContext, let’s see some more differences between them in the next section.





BeanFactory vs ApplicationContext in Spring Framework

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 files, now application context also allows Java configuration.  

In short, BeanFactory provides basic IOC and DI features while Application Context 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. Internationalization (i18n)
BeanFactory doesn't provide support for internationalization i.e. i18n but ApplicationContext provides support for it.


2. Event Publishing
Another difference between BeanFactory vs ApplicationContext is the ability to publish events to beans that are registered as listeners. For example, a ContextStartedEvent is published when the context is started and ContextStoppedEvent is published when the context is stopped. Event handling in the ApplicationContext is provided through the ApplicationEvent class and ApplicationListener interface.


3. Implementations
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. Autowiring
If you are using auto wiring and using BeanFactory then 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

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");
    hello.
sayHello("John");
}

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 the ApplicationContext interface in Spring.

And, if you want to learn more then you can further see a comprehensive Spring course like Spring MasterClass from Beginner to Expert course to understand more about the basic Spring concepts. 

What is difference between BeanFactory and ApplicationContext in Spring framework


These were some worth noting differences between BeanFactory and ApplicationContext in the 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. 


P. S. - If you are new to Spring and Spring Boot and looking for a free Spring Boot online course then I also recommend you to join the Introducing Spring Boot (FREE ) course by Dan Vega on Udemy. It's one of the best free courses to learn Spring Boot for Java developers. 

6 comments :

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.

SARAL SAXENA said...

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.

AP 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.

Thanks

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 @ https://www.springmockexams.com/




Post a Comment