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
Define a bean in an XML configuration file (
beans.xml
).Load the
BeanFactory
and retrieve the bean when needed.
Example: Loading Beans Using BeanFactory
beans.xml
B. Dependency Injection Using BeanFactory
BeanFactory automatically resolves dependencies using Constructor Injection or Setter Injection.
Example: Constructor Injection in BeanFactory
The
UserService
bean 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:
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
BeanFactory
for 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
ApplicationContext
by default (AnnotationConfigApplicationContext
,SpringApplication.run()
).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?
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
ApplicationContext
UsesDefaultListableBeanFactory
The default
ApplicationContext
in Spring Boot (AnnotationConfigApplicationContext
,WebApplicationContext
) is backed byDefaultListableBeanFactory
.This means
BeanFactory
is still the core of Spring’s bean management, just wrapped with more features.
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 toBeanFactory
for lazy loading.
Embedded & IoT Applications
When working with constrained memory environments (like IoT, cloud functions, serverless), a lightweight
BeanFactory
can be preferred over full-fledgedApplicationContext
.
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
?
ApplicationContext
is preferred, when would we explicitly use BeanFactory
?Unit Testing & Mocks – You may use
BeanFactory
for lightweight, isolated unit tests where fullApplicationContext
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?
BeanFactory
in New Applications?No – If we're using Spring Boot or standard Spring applications, we should always prefer
ApplicationContext
.Yes, but indirectly –
BeanFactory
is still used internally by Spring, but developers rarely use it explicitly.Possible Exceptions – In IoT, serverless, or special performance-optimized cases, a minimal
BeanFactory
may 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?