Disclosure: This text might include affiliate hyperlinks. Whenever you buy, we might earn a small fee.
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.
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.
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
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
public static void most important(String args[]){
ApplicationContext ctx =new ClassPathXmlApplicationContext(“beans.xml”);
Hey whats up =(Hey) ctx.getBean(“whats up”);
whats up.sayHello(“John”);
}
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.
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.
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.