Skip to main content

1.3 Auto-Configuration & Conditions

← Back to Chapter 1: Spring & Spring Boot Fundamentals

Auto-configuration is the core mechanism that makes Spring Boot "magic". It automatically configures beans in your ApplicationContext based on the JARs detected on the classpath, system properties, and custom bean definitions you have already provided.


What is Auto-Configuration?​

This is one of the most important Spring Boot concepts.

Spring Boot tries to automatically configure your application based on:

  1. Dependencies present in the classpath
  2. Application configuration
  3. Existing Beans

Simple example

You add:

spring-boot-starter-web

Spring Boot essentially says:

This application appears to be a web application, so I'll configure the required web infrastructure.

That's auto-configuration.

Does Auto-Configuration mean Spring Boot does everything automatically?​

No. Spring Boot provides sensible defaults, but you can customize them.

For example:

server.port=8081

Now the application runs on port 8081 instead of the default port.

How @EnableAutoConfiguration Operates​

@EnableAutoConfiguration This enables Spring Boot's auto-configuration mechanism. It tells Spring Boot:

"Look at the dependencies and environment and configure appropriate components automatically."

[ SpringApplication.run() ]
│
▼
[ AutoConfigurationImportSelector ]
│
▼
[ Scans META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports ]
│
▼
[ Evaluates @Conditional annotations on each candidate ]
│
▼
[ Registers only beans whose conditions EVALUATE TO TRUE ]
Web dependency
↓
Web-related auto-configuration

JPA dependency
↓
JPA-related auto-configuration

Spring Boot 3 AutoConfiguration.imports​

In Spring Boot 2.x, auto-configuration classes were registered in META-INF/spring.factories.

In Spring Boot 3+, candidate auto-configuration classes are defined in: META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

Each line lists the fully qualified class name of an auto-configuration class:

org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration

Conditional Annotations Deep Dive​

Auto-configuration works non-intrusively because it yields whenever the developer defines their own custom bean. This conditional evaluation is enabled by @Conditional derivatives:

Conditional AnnotationEvaluates True When...Common Real-World Example
@ConditionalOnClass(DataSource.class)The specified class exists on the application classpath.Configures HikariCP only if JDBC driver is included.
@ConditionalOnMissingBean(DataSource.class)The developer has not already defined a custom bean of this type.Allows user overrides: developers define custom DB, Spring backs off.
@ConditionalOnProperty(name="app.feature.enabled", havingValue="true")An external configuration property matches the specified value.Toggleable features via application.yml.
@ConditionalOnWebApplicationThe current running application is an HTTP web application.Registers DispatcherServlet only in web runtimes.
@ConditionalOnResourceA designated resource file exists on the classpath.Looks for schema.sql or data.sql.

Code Example: Custom Auto-Configuration​

@AutoConfiguration
@ConditionalOnClass(PaymentGateway.class)
public class PaymentAutoConfiguration {

@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(name = "payment.provider", havingValue = "stripe", matchIfMissing = true)
public PaymentGateway stripePaymentGateway() {
return new StripePaymentGateway();
}
}

In this example:

  1. If PaymentGateway.class is absent from the classpath, the entire configuration is skipped.
  2. If the user registers a custom PaymentGateway @Bean, Spring Boot skips creating StripePaymentGateway.
  3. If property payment.provider=paypal, this bean is not activated.