> 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/spring-features/auto-configuration/spring-boot-3-spring.factories.md).

# Spring Boot 3: spring.factories

## About

In Spring Boot 3, the traditional `META-INF/spring.factories` mechanism was replaced by a **new metadata-based system** using:

* `META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports` (for auto-configuration)
* `META-INF/spring/org.springframework.boot` folder with structured metadata files for other use cases

<figure><img src="https://3632859567-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPd6ktrA5pPLsZJktj2fm%2Fuploads%2FxqTHIjIahjq2gAJtkpoL%2Fspring-3-autocofig-1.png?alt=media&amp;token=8bdb0888-cac7-49d2-a30a-0fc939bd41c7" alt=""><figcaption></figcaption></figure>

## Why the Change ?

* **Modularity:** Split metadata per feature rather than one bulky file
* **Better performance:** Optimized classpath scanning
* **Clarity:** Easier to understand and manage for different framework extension points
* **Native image compatibility:** Improves behavior in GraalVM and AOT (Ahead-of-Time) contexts

## Replacing `EnableAutoConfiguration`

### Old Way (Spring Boot 2):

```properties
propertiesCopyEdit# spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.autoconfig.MyAutoConfiguration
```

### New Way (Spring Boot 3):

Create the file:

```
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
```

Add your class:

```
com.example.autoconfig.MyAutoConfiguration
```

Java Class:

```java
@Configuration
@ConditionalOnClass(name = "com.example.MyService")
public class MyAutoConfiguration {

    @Bean
    public MyService myService() {
        return new MyService();
    }
}
```

{% hint style="success" %}
This is now the standard way to register auto-configuration classes in Spring Boot 3.
{% endhint %}

## ApplicationContextInitializer, EnvironmentPostProcessor, RunListener

These are **not** moved to a new system. They **still use `spring.factories`**, even in Spring Boot 3.

So, for the following types, **we still use `META-INF/spring.factories`**:

#### Still supported in Spring Boot 3 via `spring.factories`:

| Interface/Class                 | Description                               |
| ------------------------------- | ----------------------------------------- |
| `ApplicationContextInitializer` | Modify application context before refresh |
| `EnvironmentPostProcessor`      | Add/override environment properties       |
| `ApplicationListener`           | Hook into Spring event lifecycle          |
| `SpringApplicationRunListener`  | Listen to Spring Boot startup phases      |

### Example:

```properties
# META-INF/spring.factories
org.springframework.boot.env.EnvironmentPostProcessor=\
com.example.env.MyEnvPostProcessor
```

```java
public class MyEnvPostProcessor implements EnvironmentPostProcessor {
    public void postProcessEnvironment(ConfigurableEnvironment env, SpringApplication app) {
        env.getSystemProperties().put("env.source", "custom");
    }
}
```

## Comparison

<table data-full-width="true"><thead><tr><th width="185.3515625">Feature</th><th width="461.55078125">Spring Boot 2 (Old)</th><th>Spring Boot 3 (New)</th></tr></thead><tbody><tr><td>Auto Configuration</td><td><code>spring.factories</code> with <code>EnableAutoConfiguration</code> key</td><td><code>AutoConfiguration.imports</code> file</td></tr><tr><td>Initializers</td><td><code>spring.factories</code></td><td>still <code>spring.factories</code></td></tr><tr><td>Listeners</td><td><code>spring.factories</code></td><td>still <code>spring.factories</code></td></tr><tr><td>Env Post Processor</td><td><code>spring.factories</code></td><td>still <code>spring.factories</code></td></tr><tr><td>Lifecycle Hooks</td><td><code>spring.factories</code></td><td>still <code>spring.factories</code></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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://www.pranaypourkar.co.in/the-programmers-guide/spring/spring-features/auto-configuration/spring-boot-3-spring.factories.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
