BeanFactory vs ApplicationContext in Spring: When to Use Which?

This article compares Spring's BeanFactory and ApplicationContext, explaining their roles in retrieving beans, detailing lazy versus eager loading, providing Java and XML code examples, and offering guidance on when to choose each interface for efficient backend development.

Programmer DD
Programmer DD
Programmer DD
BeanFactory vs ApplicationContext in Spring: When to Use Which?

BeanFactory and ApplicationContext are both used to obtain Spring beans from the container, but they differ significantly.

What Is a Spring Bean

A Spring bean is a Java object managed by the Spring container. Below is a simple example.

package com.zoltanraffai;
public class HelloWorld {
    private String message;
    public void setMessage(String message) {
        this.message = message;
    }
    public void getMessage() {
        System.out.println("My Message : " + message);
    }
}

In XML‑based configuration, beans.xml provides the metadata for managing beans.

What Is the Spring Container

The Spring container is responsible for instantiating, configuring, and assembling beans. Here is an example of configuring the HelloWorld POJO.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <bean id="helloWorld" class="com.zoltanraffai.HelloWorld">
        <property name="message" value="Hello World!"/>
    </bean>
</beans>

Now the bean is managed by the Spring container. How do we retrieve it?

BeanFactory vs ApplicationContext

BeanFactory Interface

BeanFactory is the root interface for accessing the Spring container. It typically uses lazy loading, meaning beans are instantiated only when getBean() is called. The most common implementation is XmlBeanFactory.

Bean instantiation/wiring

package com.zoltanraffai;
import org.springframework.core.io.ClassPathResource;
import org.springframework.beans.factory.xml.XmlBeanFactory;
public class HelloWorldApp {
    public static void main(String[] args) {
        XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("beans.xml"));
        HelloWorld obj = (HelloWorld) factory.getBean("helloWorld");
        obj.getMessage();
    }
}

ApplicationContext Interface

ApplicationContext is the central interface in a Spring application, providing configuration information and extending BeanFactory with additional features such as automatic registration of BeanPostProcessor and BeanFactoryPostProcessor, internationalization support, and event publishing. Unlike BeanFactory, it pre‑loads all singleton beans at startup.

Bean instantiation/wiring

Automatic BeanPostProcessor registration

Automatic BeanFactoryPostProcessor registration

Convenient MessageSource access (i18n)

ApplicationEvent publishing

package com.zoltanraffai;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloWorldApp {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
        obj.getMessage();
    }
}

Summary

ApplicationContext includes all features of BeanFactory and is generally recommended for most enterprise applications. However, in memory‑constrained scenarios such as mobile apps, the lighter‑weight BeanFactory may be more appropriate.

Soul Questions

How to use BeanPostProcessor and BeanFactoryPostProcessor?

Do you understand the Spring Bean lifecycle? Knowing this greatly helps in using beans effectively.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaBackend Developmentspringdependency-injectionapplicationcontextBeanFactory
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.