Quick revision
The short answer
BeanFactory is the basic container: it holds bean definitions, creates beans and injects their dependencies, lazily when someone asks for them. ApplicationContext builds on it and pre-creates all singletons at refresh, so configuration errors show up at startup. It also detects BeanPostProcessors and BeanFactoryPostProcessors automatically and adds the Environment, event publishing, a MessageSource for messages and resource loading. Applications use an ApplicationContext; BeanFactory is mostly used inside it.
What to remember
In this answer, we saw that BeanFactory provides basic bean creation and dependency injection and creates beans lazily on request. ApplicationContext extends it: it eagerly creates singletons at refresh so configuration errors appear at startup, detects and registers BeanPostProcessors and BeanFactoryPostProcessors automatically, and adds the Environment, event publishing, a MessageSource for internationalisation and resource loading. Applications almost always use an ApplicationContext, while BeanFactory works inside it.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
BeanFactory is the basic container: it holds bean definitions, creates beans and injects their dependencies, lazily when someone asks for them. ApplicationContext builds on it and pre-creates all singletons at refresh, so configuration errors show up at startup. It also detects BeanPostProcessors and BeanFactoryPostProcessors automatically and adds the Environment, event publishing, a MessageSource for messages and resource loading. Applications use an ApplicationContext; BeanFactory is mostly used inside it.
Why this question is asked
Almost no one writes new XmlBeanFactory() any more, so why do interviewers still ask this? Because the differences explain everyday behaviour: why a missing bean fails at startup instead of at the first request, why @Autowired and @Transactional work at all, and where events and properties come from. In this answer, we will recap what each interface is, look at eager versus lazy creation with a small experiment, see how post-processors are registered, walk through the extra services the context provides, and see how the two relate in a running Spring Boot application.
1A Quick Recap of the Two Interfaces
BeanFactory is the root interface of the Spring container. It defines methods such as getBean(name), getBean(type), containsBean and isSingleton. Its main implementation, DefaultListableBeanFactory, stores bean definitions, the recipes that say how to create each bean, and creates beans from them, resolving their dependencies.
ApplicationContext is a sub-interface of BeanFactory. Every ApplicationContext is a BeanFactory, and in fact the usual implementations hold a DefaultListableBeanFactory inside and delegate bean work to it. What the context adds is a complete application environment around that factory.
2Eager Singletons: Errors at Startup
A plain BeanFactory creates a bean only when getBean() asks for it. An ApplicationContext, during refresh(), creates every non-lazy singleton up front. The experiment below shows the difference with a bean whose constructor fails.
So with a context, a wrong property, a missing dependency or a bad constructor makes the application fail to start, which is exactly what we want in a deployment: a failed health check instead of an error at the first customer request. Beans can still opt into laziness with @Lazy, or globally with spring.main.lazy-initialization=true in Spring Boot, but then we accept late errors in exchange for faster startup.
3Post-Processors Are Registered for Us
Much of Spring's behaviour comes from two kinds of extension beans. A BeanFactoryPostProcessor runs after definitions are loaded and before any bean is created, and can change definitions; ConfigurationClassPostProcessor, which reads @Configuration classes and runs component scanning, is one, and PropertySourcesPlaceholderConfigurer, which resolves ${...} placeholders, is another. A BeanPostProcessor runs around the creation of each bean; AutowiredAnnotationBeanPostProcessor fills @Autowired and @Value, and the proxy creators add the wrappers for @Transactional and @Async.
An ApplicationContext finds all such beans among its definitions and runs them automatically during refresh. A plain BeanFactory does not. With a bare DefaultListableBeanFactory, we must call addBeanPostProcessor ourselves, otherwise @Autowired fields stay null and no proxies are created. This is the single biggest practical reason no application uses a bare BeanFactory.
4The Extra Services an ApplicationContext Provides
Beyond bean creation, the context gives our code four services. The Environment holds profiles and property sources, such as application.yml, environment variables and system properties, and resolves values for @Value and configuration properties. Event publishing lets beans publish an object with ApplicationEventPublisher.publishEvent and lets @EventListener methods receive it, which also delivers lifecycle events such as ContextRefreshedEvent. A MessageSource resolves localised messages from files such as messages_fr.properties, used for validation messages and internationalisation. And resource loading turns strings such as classpath:templates/mail.html or file:/etc/app/key.pem into Resource objects, including patterns with wildcards.
| Feature | BeanFactory | ApplicationContext |
|---|---|---|
| Bean creation and injection | yes | yes, delegates to an inner factory |
| Singleton creation | lazy, on getBean() | eager at refresh, unless @Lazy |
| Auto-registers post-processors | no, add by hand | yes |
| Environment and profiles | no | yes |
| Events | no | yes |
MessageSource (i18n) | no | yes |
| Resource loading with patterns | no | yes |
Notice that each of these is injected like any other bean; the context registers them for us.
5How They Relate in a Real Application
In a Spring Boot application, SpringApplication.run creates an ApplicationContext, for example a servlet web server context, and that context holds a DefaultListableBeanFactory. We work with the context, and the context uses the factory internally. Occasionally we see BeanFactory directly: a BeanFactoryPostProcessor receives a ConfigurableListableBeanFactory, and code that needs to look beans up lazily may inject ObjectProvider, which is backed by the factory.
So the rule is simple. Applications use ApplicationContext. BeanFactory is the engine inside and the extension point for framework-level code, and it is worth knowing mainly because it explains how the context does its work.
6Follow-up questions
Why would anyone use a plain BeanFactory today?
Almost never in application code. It made sense years ago on memory-constrained devices. Today we meet it inside the framework, in a BeanFactoryPostProcessor or BeanFactoryAware callback, or in small tests of container internals.
Does lazy initialisation make an ApplicationContext behave like a BeanFactory?
Only for creation timing. With lazy initialisation enabled, beans are created on first use, so errors move to that moment, but post-processors, the Environment, events and the other services still work. It is a startup optimisation, not a switch to a different container.
Can a bean get hold of the ApplicationContext itself?
Yes, by injecting ApplicationContext or implementing ApplicationContextAware. It is fine for framework-style code, but using it to call getBean from business code turns the container into a service locator, so it is better to inject what we need directly.
7Conclusion
In this answer, we saw that BeanFactory provides basic bean creation and dependency injection and creates beans lazily on request. ApplicationContext extends it: it eagerly creates singletons at refresh so configuration errors appear at startup, detects and registers BeanPostProcessors and BeanFactoryPostProcessors automatically, and adds the Environment, event publishing, a MessageSource for internationalisation and resource loading. Applications almost always use an ApplicationContext, while BeanFactory works inside it.
Open with
BeanFactory is the core container interface: it holds bean definitions, creates beans and wires their dependencies, and a plain one creates beans lazily, only when getBean is called.
Then, if they want more
ApplicationContext extends BeanFactory, and the usual implementations actually delegate to a DefaultListableBeanFactory inside. What it adds matters a lot in practice. First, at refresh it pre-creates every non-lazy singleton, so a bad property or a missing dependency fails the startup, not the first request. Second, it finds BeanFactoryPostProcessors and BeanPostProcessors among the beans and runs them automatically.
That is what makes @Configuration parsing, placeholders, @Autowired and the proxies for @Transactional work; with a bare BeanFactory you would have to register those processors yourself, or your @Autowired fields stay null.
Third, it provides application services: the Environment with profiles and property sources, event publishing with @EventListener, a MessageSource for localised messages, and a ResourceLoader for classpath and file resources. In a Spring Boot app we always work with an ApplicationContext. I only meet BeanFactory directly in framework-level code, such as a BeanFactoryPostProcessor, or when I use ObjectProvider for lazy lookups.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Core technologies
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What does ApplicationContext add on top of BeanFactory?": https://scrivio-interview-library.netlify.app/spring-boot/ioc-container/26130dae67/