Spring Framework FAQ: Top Questions
43. What is the difference between BeanFactory and ApplicationContext?
BeanFactory
is the root container interface in Spring, while ApplicationContext
is a more advanced child interface that adds support for i18n, events, and resource loading.
📘 Key Differences:
ApplicationContext
eagerly loads beans;BeanFactory
loads lazily.ApplicationContext
supports event publication, internationalization, and auto-wiring.
📥 Example:
BeanFactory factory = new XmlBeanFactory(new ClassPathResource("beans.xml"));
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
🏆 Expected Output:
Both create bean instances, but ApplicationContext offers more features.
🛠️ Use Cases:
- Use ApplicationContext for modern Spring apps.
- BeanFactory only for memory-constrained environments.