BeanFactory vs ApplicationContext in Spring

In this short article, we will quickly discuss the differences between the BeanFactory and ApplicationContext container levels and the implications of bootstrapping.
 
The Spring container is responsible for instantiating, configuring, and assembling the Spring beans. The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata. The configuration metadata is represented in XML, Java annotations, or Java code. It lets you express the objects that compose your application and the rich interdependencies between those objects.
 
Both BeanFactory and ApplicationContext provide a way to get a bean from the Spring IOC container by calling getBean("bean name"), but there is some difference in their working and features provided by them.
 
The below diagram shows the implementations of BeanFactory and ApplicationContext interfaces:

1. Basic Overview

BeanFactory

  • It is the root interface for accessing the Spring container.
  • Provides basic IoC container functionalities.

ApplicationContext: 

  • A sub-interface of BeanFactory.
  • Provides advanced features on top of basic IoC functionalities.

2. Features

BeanFactory: 

Basic IoC Features: Creates and manages beans and their dependencies.

Lazy Initialization: By default, beans are lazily initialized. This means they are instantiated only when explicitly requested.

Resource Handling: Uses Resource interface to access resources.

ApplicationContext: 

All BeanFactory Features: Inherits all functionalities of BeanFactory.

Event Propagation: Supports event propagation to beans by implementing the ApplicationListener interface.

Internationalization: Provides message resource handling for internationalization.

Web Application Context: Has a variant for web-based applications, which understands Servlet contexts.

Eager Initialization: By default, singletons are eagerly initialized to detect bean configuration issues early.

3. Examples

The BeanFactory interface Example

package net.javaguides.spring.ioc;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Application {
    public static void main(String[] args) {
        XmlBeanFactory factory = new XmlBeanFactory (new ClassPathResource("applicationContext.xml"));
    }
}

The ApplicationContext interface Example

package net.javaguides.spring.ioc;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Application {
    public static void main(String[] args) {
        ApplicationContext  context = new AnnotationConfigApplicationContext(AppConfig.class);
        context.close();
    }
}

4. Why use ApplicationContext over BeanFactory?

While BeanFactory serves its purpose by providing basic IoC container functionalities, ApplicationContext is more feature-rich. 

Here are reasons you might prefer it:

Ease of Configuration: ApplicationContext can integrate with Spring's AOP features, which is beneficial for applying cross-cutting concerns like logging and transactions.

Built-in Enterprise Features: ApplicationContext supports internationalization, JNDI access, event propagation, and various application layers, making it a more holistic choice for enterprise applications.

Web Support: If you're building a web application, the WebApplicationContext is an extension of ApplicationContext tailored for such environments.

5. When to use BeanFactory?

Given ApplicationContext's advantages, you might wonder if there's ever a need for BeanFactory. The answer is nuanced:

Resource Constraints: If your application runs in an environment with tight memory or CPU constraints, the lightweight nature of BeanFactory might make it more suitable.

Fine-grained Control: If you require more control over bean initialization, especially the lazy initialization aspect, BeanFactory is beneficial.

6. Conclusion

Both BeanFactory and ApplicationContext play crucial roles in the Spring ecosystem. While BeanFactory provides the fundamentals, ApplicationContext is a more advanced container that offers additional features beneficial for most modern applications. It's essential to choose the right component based on the specific needs of your project. As a general recommendation, for most standard applications, ApplicationContext should be the default choice given its comprehensive set of features.

Comments