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,- BeanFactoryinitializes 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
- Define a bean in an XML configuration file ( - beans.xml).
- Load the - BeanFactoryand 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 - UserServicebean automatically gets the UserRepository dependency injected.
Types of BeanFactory Implementations
A. DefaultListableBeanFactory (Most Common)
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)
XmlBeanFactory (Deprecated)- Loads bean definitions from an XML file. 
- Deprecated in Spring 5 in favor of - ApplicationContext.
C. SimpleBeanFactory
SimpleBeanFactory- A minimal implementation of - BeanFactoryfor unit testing and basic applications.
Is BeanFactory Used in Latest Spring Applications?
BeanFactory Used in Latest Spring Applications?In modern Spring applications, BeanFactory is usually not used explicitly because:
- XML-based configuration is mostly replaced by annotations ( - @Component,- @Bean) and Java-based configuration (- @Configuration).
- Spring Boot applications use - ApplicationContextby default (- AnnotationConfigApplicationContext,- SpringApplication.run()).
- Features like event handling, internationalization, and AOP are needed in most real-world applications, and - ApplicationContextprovides them.
However, BeanFactory is still used internally in Spring.
How is BeanFactory Used Internally in Modern Spring?
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:
- Spring Boot's - ApplicationContextUses- DefaultListableBeanFactory- The default - ApplicationContextin Spring Boot (- AnnotationConfigApplicationContext,- WebApplicationContext) is backed by- DefaultListableBeanFactory.
- This means - BeanFactoryis still the core of Spring’s bean management, just wrapped with more features.
 
- Lazy Bean Loading (Efficient Memory Management) - BeanFactoryis still relevant in large-scale applications where lazy initialization is needed for performance reasons.
- Even - ApplicationContextdelegates to- BeanFactoryfor lazy loading.
 
- Embedded & IoT Applications - When working with constrained memory environments (like IoT, cloud functions, serverless), a lightweight - BeanFactorycan be preferred over full-fledged- ApplicationContext.
 
- Custom Frameworks Based on Spring - Some custom dependency injection frameworks built on Spring still use - BeanFactoryto manage lightweight configurations.
 
If ApplicationContext is preferred, when would we explicitly use BeanFactory?
ApplicationContext is preferred, when would we explicitly use BeanFactory?- Unit Testing & Mocks – You may use - BeanFactoryfor lightweight, isolated unit tests where full- ApplicationContextis unnecessary.
- Memory-Constrained Environments – If you’re running Spring in an embedded system or microservice with limited RAM, using - BeanFactorymight improve performance.
- Lazy Initialization for Performance Optimization – If you want strict lazy loading, - BeanFactoryprovides the best mechanism.
- Custom Bean Loaders – If you need on-demand bean loading without full application context, - BeanFactorycan be useful.
Should we use BeanFactory in New Applications?
BeanFactory in New Applications?- No – If we're using Spring Boot or standard Spring applications, we should always prefer - ApplicationContext.
- Yes, but indirectly – - BeanFactoryis 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
