> 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="/files/7X2cD48u9US3bg8aZ4xW" 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 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/spring-features/auto-configuration/spring-boot-3-spring.factories.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.
