> For the complete documentation index, see [llms.txt](https://www.pranaypourkar.co.in/the-programmers-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.pranaypourkar.co.in/the-programmers-guide/spring/core-concepts/spring-beans/beanfactory.md).

# 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**

```java
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**

```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**

```xml
<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:

  ```java
  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.

{% hint style="info" %}

### **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 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
{% endhint %}

## **BeanFactory vs ApplicationContext**

<table data-header-hidden data-full-width="true"><thead><tr><th></th><th></th><th></th></tr></thead><tbody><tr><td>Feature</td><td><strong>BeanFactory</strong> 🏗</td><td><strong>ApplicationContext</strong> 🚀</td></tr><tr><td><strong>Primary Use</strong></td><td>Core DI container</td><td>Full-featured Spring container</td></tr><tr><td><strong>Configuration Style</strong></td><td>XML-based (old)</td><td>Annotations + Java-based (modern)</td></tr><tr><td><strong>Lazy Initialization</strong></td><td>Yes (by default)</td><td>No (by default, eager initialization)</td></tr><tr><td><strong>Event Handling</strong></td><td>No</td><td>Yes (ApplicationEvent, listeners)</td></tr><tr><td><strong>Internationalization (i18n)</strong></td><td>No</td><td>Yes (MessageSource support)</td></tr><tr><td><strong>AOP Support</strong></td><td>No</td><td>Yes (Proxies, AspectJ)</td></tr><tr><td><strong>Resource Handling</strong></td><td>Limited</td><td>Supports file, URL, streams, etc.</td></tr><tr><td><strong>Usage in Modern Apps?</strong></td><td>Rare (internal use)</td><td>Default in Spring Boot apps</td></tr><tr><td><strong>Best For?</strong></td><td>Lightweight, memory-efficient cases</td><td>Standard Spring applications</td></tr></tbody></table>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://www.pranaypourkar.co.in/the-programmers-guide/spring/core-concepts/spring-beans/beanfactory.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
