BeanFactory vs ApplicationContext
`BeanFactory` is Spring's basic container abstraction for creating and managing beans. `ApplicationContext` builds on it and provides additional application-level capabilities such as event publication, message resolution, resource loading, and integration with other Spring infrastructure.
`BeanFactory` defines the fundamental IoC container operations: obtaining beans, checking whether beans exist, and managing their lifecycle according to the configured bean definitions. It is intentionally minimal and is useful when only core dependency injection and bean management are required.
`ApplicationContext` extends the `BeanFactory` contract and adds features commonly needed by complete applications. These include an event mechanism, internationalized messages through `MessageSource`, resource loading, and support for registering and discovering application components and infrastructure.
In typical Spring applications, developers work with an `ApplicationContext` rather than directly creating a `BeanFactory`. Common implementations include `AnnotationConfigApplicationContext` for Java configuration and `ClassPathXmlApplicationContext` for XML configuration.
The distinction is not simply that one creates beans lazily and the other eagerly in every case. Bean creation timing depends on bean definitions, scopes, and configuration. For interview questions, focus on the API hierarchy and the additional services supplied by `ApplicationContext`.
A useful rule is that `ApplicationContext` is the usual application-level container, while `BeanFactory` represents the smaller core container abstraction underneath it.
Key points
- `ApplicationContext` extends the `BeanFactory` interface.
- `BeanFactory` provides core IoC and bean-management functionality.
- `ApplicationContext` adds events, message resolution, and resource loading.
- Most ordinary Spring applications use an `ApplicationContext`.
- Bean initialization timing should not be reduced to a simple BeanFactory-versus-ApplicationContext rule.
Interview questions
What additional features does ApplicationContext provide?
An `ApplicationContext` provides several facilities beyond basic bean lookup and lifecycle management. Important examples include application events, internationalized message resolution through `MessageSource`, and resource loading. It also integrates with other Spring infrastructure, making it a practical application-level container.
What is the difference between BeanFactory and ApplicationContext?
`BeanFactory` is the basic Spring IoC container abstraction responsible for bean creation and management. `ApplicationContext` extends `BeanFactory` and adds application-level facilities such as event publication, message resolution, and resource loading. In normal Spring applications, `ApplicationContext` is commonly used because these additional services are useful beyond basic dependency injection.
Does ApplicationContext replace BeanFactory?
It is better to say that `ApplicationContext` extends the `BeanFactory` abstraction rather than replaces it. It inherits the core bean-management capabilities and adds functionality needed by application code. Therefore, code written against the `BeanFactory` contract can generally work with an `ApplicationContext` as well.
Is BeanFactory lazy and ApplicationContext eager?
That is an oversimplification. Bean creation timing depends on the bean definition, scope, and relevant configuration rather than solely on which interface is used. ApplicationContext commonly initializes singleton beans during context startup, but configuration can affect initialization behavior, so the distinction should not be presented as an absolute rule.
Why would a typical Spring application use ApplicationContext instead of BeanFactory?
A typical application needs more than basic dependency injection. `ApplicationContext` supplies features such as events, message resolution, resource access, and integration with Spring infrastructure while retaining the core bean-container functionality. Using it therefore provides the standard application-level programming model without giving up the capabilities inherited from `BeanFactory`.