Monday, March 27, 2023
HomeJavaDistinction between BeanFactory and ApplicationContext in Spring Framework

Distinction between BeanFactory and ApplicationContext in Spring Framework

Disclosure: This text might include affiliate hyperlinks. Whenever you buy, we might earn a small fee.

The distinction between BeanFactory and ApplicationContext in Spring framework is one other ceaselessly requested Spring interview query principally requested Java programmers with 2 to 4 years expertise in Java and Spring. Each BeanFactory and ApplicationContext present a approach to get a bean from the Spring IOC container by calling getBean(“bean title”), however there may be some distinction of their working and options offered by them. One distinction between the bean manufacturing unit and software context is that the previous solely instantiates bean while you name getBean() technique whereas ApplicationContext instantiates Singleton bean when the container is began,  It would not watch for the getBean to be known as. 

These interview questions are third on my record of ceaselessly requested spring questions e.g. Setter vs Constructor Injection and 
What’s the default scope of Spring bean. In case you are making ready for a Java interview and anticipating some Spring framework questions, It’s price making ready these questions. 

By the best way, in case you are new to the Spring framework then I additionally counsel you be part of a complete and up-to-date course to be taught Spring in depth. In case you want suggestions, I extremely counsel you check out Spring Framework: Newbie to Guru, one of many complete and hands-on programs to be taught trendy Spring. It’ additionally probably the most up-to-date and covers Spring 5.

Coming again to BeanFactory vs ApplicationContext, let’s see some extra variations between them within the subsequent part.

BeanFactory vs ApplicationContext in Spring Framework

Earlier than seeing the distinction between ApplicationContext and BeanFactory, let see some similarity between each of them. Spring gives two sorts of IOC containers, one is BeanFactory, and the opposite is ApplicationContext.

Syntactically BeanFactory and ApplicationContext each are Java interfaces and ApplicationContext extends BeanFactory.  Each of them are configuration utilizing XML configuration information, now software context additionally permits Java configuration.  

Briefly, BeanFactory gives primary IOC and DI options whereas Software Context gives superior options. Other than these, listed here are just a few extra variations between BeanFactory and ApplicationContext which is principally primarily based upon options supported by them.

1. Internationalization (i18n)

BeanFactory would not present help for internationalization i.e. i18n however ApplicationContext gives help for it.

2. Occasion Publishing

One other distinction between BeanFactory vs ApplicationContext is the flexibility to publish occasions to beans which are registered as listeners. For instance, a ContextStartedEvent is revealed when the context is began and ContextStoppedEvent is revealed when the context is stopped. Occasion dealing with within the ApplicationContext is offered by the ApplicationEvent class and ApplicationListener interface.

3. Implementations

One of many fashionable implementations of the BeanFactory interface is XMLBeanFactory whereas one of many fashionable implementations of the ApplicationContext interface is ClassPathXmlApplicationContext. On Java internet software we use WebApplicationContext which extends the ApplicationContext interface and provides the getServletContext technique.

4. Autowiring

In case you are utilizing auto wiring and utilizing BeanFactory then you should register AutoWiredBeanPostProcessor utilizing API which you’ll be able to configure in XML in case you are utilizing  ApplicationContext. 

In abstract, BeanFactory is OK for testing and non-production use however ApplicationContext is extra feature-rich container implementation and must be favored over BeanFactory

I principally use XML configuration file and ClassPathXmlApplicationContext to rapidly run any Spring-based Java program from Eclipse  by utilizing the next snippet of code :

public static void most important(String args[]){
    ApplicationContext ctx =
new ClassPathXmlApplicationContext(“beans.xml”);
    Hey whats up =
(Hey) ctx.getBean(“whats up”);
    whats up.

right here beans.xml is your spring configuration file and “whats up” is a bean outlined in that spring configuration file. Right here now we have used ClassPathXmlApplicationContext which is an implementation of the ApplicationContext interface in Spring.

What is difference between BeanFactory and ApplicationContext in Spring framework

These have been some price noting variations between BeanFactory and ApplicationContext within the Spring framework. In most sensible instances you can be utilizing ApplicationContext however understanding about BeanFactory is necessary to know the basic idea of the spring framework. 

Different Spring MVC articles you could prefer to discover

P. S. – In case you are new to Spring and Spring Boot and in search of a
free Spring Boot on-line course then I additionally suggest you to affix the Introducing Spring Boot (FREE ) course by Dan Vega on Udemy. It is top-of-the-line free programs to be taught Spring Boot for Java builders. 



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments