> 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.md).

# Auto Configuration

## About

Auto-configuration is a feature in Spring Boot that **automatically configures our application based on the dependencies present in the classpath**.

We don’t have to manually configure beans for commonly used components like:

* DataSource
* Spring MVC
* Security
* JPA
* RabbitMQ, Kafka, etc.

If Spring Boot detects related libraries in the classpath, it tries to auto-configure them with sensible defaults.

{% hint style="info" %}

* `@EnableAutoConfiguration`: Tells Spring Boot to start auto-configuring beans.
* `@SpringBootApplication`: Includes `@EnableAutoConfiguration`, `@ComponentScan`, and `@Configuration`.
* `@ConditionalOnClass`, `@ConditionalOnMissingBean`, `@ConditionalOnProperty`: Used in internal auto-config classes to decide what should be created.
  {% endhint %}

## Why Is It Useful?

Auto-Configuration:

* Reduces boilerplate code
* Speeds up development
* Helps beginners start with minimal setup
* Makes applications cleaner and easier to manag

## How It Works ?

1. We annotate our main class with `@SpringBootApplication`, which includes `@EnableAutoConfiguration`.
2. Spring Boot scans the classpath and configuration properties.
3. It loads auto-configuration classes declared in:

   ```
   META-INF/spring.factories (Spring Boot 2.x)
   ```

   or

   ```
   META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports (Spring Boot 3.x)
   ```
4. Each auto-configuration class has a series of `@Conditional...` annotations.
5. If conditions match (e.g., a class is present, a property is set), Spring Boot applies that configuration.

### Example

If we include **Spring Web** in our project (`spring-boot-starter-web`), Spring Boot will:

* Detect `spring-webmvc` on the classpath
* Auto-configure:
  * DispatcherServlet
  * Default error pages
  * Jackson for JSON serialization
  * Tomcat server setup

No need for us to declare those manually.

## How to Disable Auto-Configuration ?

We can selectively disable specific auto-configurations like this:

```java
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
```

## Best Practices

* Rely on Spring Boot’s default auto-configs as much as possible.
* Only create our own when needed (e.g., for reusable modules or libraries).
* Use `@Conditional` annotations to avoid overriding user-defined beans.
* Use `debug=true` to understand what’s going on.


---

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