BeanFactory

About

BeanFactory is the fundamental container in Spring for managing and configuring beans. It instantiates, configures, and manages bean objects in a Spring application.

It is the simplest and lightweight container in Spring, designed for basic dependency injection (DI) and bean lifecycle management. While ApplicationContext is more commonly used, BeanFactory remains important in memory-constrained environments and advanced scenarios like lazy loading.

BeanFactory is an interface in the Spring Framework that acts as the core container for managing beans. It is part of the org.springframework.beans.factory package and follows the Factory Design Pattern to provide a mechanism for instantiating, configuring, and retrieving beans.

Responsibilities of BeanFactory

  • Creates and Manages Beans – Responsible for instantiating, configuring, and wiring beans in a Spring application.

  • Dependency Injection – Resolves dependencies between beans and injects them automatically.

  • Bean Lifecycle Management – Handles initialization, destruction, and scope management of beans.

  • Lazy Loading Support – Unlike ApplicationContext, BeanFactory initializes beans only when requested, improving startup performance.

How Does BeanFactory Work?

Although BeanFactory primarily relies on XML-based configuration, it can work with annotation-based dependency injection. However, unlike ApplicationContext, BeanFactory does not automatically scan for components annotated with @Component, @Service, or @Repository. We need to explicitly register beans or use ClassPathBeanDefinitionScanner to detect them.

A. Basic Usage of BeanFactory

  1. Define a bean in an XML configuration file (beans.xml).

  2. Load the BeanFactory and retrieve the bean when needed.

Example: Loading Beans Using BeanFactory

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class BeanFactoryExample {
    public static void main(String[] args) {
        // Load XML configuration file
        BeanFactory factory = new XmlBeanFactory(new ClassPathResource("beans.xml"));

        // Retrieve bean from BeanFactory
        MyBean myBean = (MyBean) factory.getBean("myBean");
        myBean.displayMessage();
    }
}

beans.xml

<beans>
    <bean id="myBean" class="com.example.MyBean"/>
</beans>

B. Dependency Injection Using BeanFactory

BeanFactory automatically resolves dependencies using Constructor Injection or Setter Injection.

Example: Constructor Injection in BeanFactory

<bean id="userService" class="com.example.UserService">
    <constructor-arg ref="userRepository"/>
</bean>

<bean id="userRepository" class="com.example.UserRepository"/>
  • The UserService bean automatically gets the UserRepository dependency injected.

Types of BeanFactory Implementations

A. DefaultListableBeanFactory (Most Common)

  • The most commonly used default implementation of BeanFactory.

  • Supports Autowiring, Bean Definitions, and Dependency Checking.

  • Example:

    DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();

B. XmlBeanFactory (Deprecated)

  • Loads bean definitions from an XML file.

  • Deprecated in Spring 5 in favor of ApplicationContext.

C. SimpleBeanFactory

  • A minimal implementation of BeanFactory for unit testing and basic applications.

Is BeanFactory Used in Latest Spring Applications?

In modern Spring applications, BeanFactory is usually not used explicitly because:

  1. XML-based configuration is mostly replaced by annotations (@Component, @Bean) and Java-based configuration (@Configuration).

  2. Spring Boot applications use ApplicationContext by default (AnnotationConfigApplicationContext, SpringApplication.run()).

  3. Features like event handling, internationalization, and AOP are needed in most real-world applications, and ApplicationContext provides them.

However, BeanFactory is still used internally in Spring.

How is BeanFactory Used Internally in Modern Spring?

Even though developers don’t use BeanFactory directly, Spring internally uses it. Some areas where it plays a role:

  1. Spring Boot's ApplicationContext Uses DefaultListableBeanFactory

    • The default ApplicationContext in Spring Boot (AnnotationConfigApplicationContext, WebApplicationContext) is backed by DefaultListableBeanFactory.

    • This means BeanFactory is still the core of Spring’s bean management, just wrapped with more features.

  2. Lazy Bean Loading (Efficient Memory Management)

    • BeanFactory is still relevant in large-scale applications where lazy initialization is needed for performance reasons.

    • Even ApplicationContext delegates to BeanFactory for lazy loading.

  3. Embedded & IoT Applications

    • When working with constrained memory environments (like IoT, cloud functions, serverless), a lightweight BeanFactory can be preferred over full-fledged ApplicationContext.

  4. Custom Frameworks Based on Spring

    • Some custom dependency injection frameworks built on Spring still use BeanFactory to manage lightweight configurations.

If ApplicationContext is preferred, when would we explicitly use BeanFactory?

  • Unit Testing & Mocks – You may use BeanFactory for lightweight, isolated unit tests where full ApplicationContext is unnecessary.

  • Memory-Constrained Environments – If you’re running Spring in an embedded system or microservice with limited RAM, using BeanFactory might improve performance.

  • Lazy Initialization for Performance Optimization – If you want strict lazy loading, BeanFactory provides the best mechanism.

  • Custom Bean Loaders – If you need on-demand bean loading without full application context, BeanFactory can be useful.

Should we use BeanFactory in New Applications?

  • No – If we're using Spring Boot or standard Spring applications, we should always prefer ApplicationContext.

  • Yes, but indirectlyBeanFactory is still used internally by Spring, but developers rarely use it explicitly.

  • Possible Exceptions – In IoT, serverless, or special performance-optimized cases, a minimal BeanFactorymay be used.

For 99% of Spring Boot applications, ApplicationContext is the right choice

BeanFactory vs ApplicationContext

Feature

BeanFactory 🏗

ApplicationContext 🚀

Primary Use

Core DI container

Full-featured Spring container

Configuration Style

XML-based (old)

Annotations + Java-based (modern)

Lazy Initialization

Yes (by default)

No (by default, eager initialization)

Event Handling

No

Yes (ApplicationEvent, listeners)

Internationalization (i18n)

No

Yes (MessageSource support)

AOP Support

No

Yes (Proxies, AspectJ)

Resource Handling

Limited

Supports file, URL, streams, etc.

Usage in Modern Apps?

Rare (internal use)

Default in Spring Boot apps

Best For?

Lightweight, memory-efficient cases

Standard Spring applications

Last updated

Was this helpful?