Spring Boot’s auto-configuration mechanism inspects your classpath, the beans you have already defined, and various property settings to automatically configure the Spring application context on your behalf. When HSQLDB is on the classpath and you have not registered aDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/spring-projects/spring-boot/llms.txt
Use this file to discover all available pages before exploring further.
DataSource bean yourself, Spring Boot configures an in-memory database without you writing a single line of configuration.
Enabling auto-configuration
Auto-configuration is opt-in. You activate it by placing@EnableAutoConfiguration or @SpringBootApplication on one of your @Configuration classes.
@SpringBootApplication is a convenience annotation that combines three annotations in one:
@SpringBootConfiguration
Marks the class as a configuration source and enables detection in integration tests.
@EnableAutoConfiguration
Activates Spring Boot’s auto-configuration import selector, which loads all registered auto-configurations.
@ComponentScan
Scans the package of the annotated class and its sub-packages for Spring components.
How conditions control auto-configuration
Each auto-configuration class is a regular@Configuration class annotated with one or more @Conditional annotations. Spring Boot evaluates these conditions at startup and only applies an auto-configuration when all its conditions are met.
The three most commonly used condition annotations are:
@ConditionalOnClass
@ConditionalOnClass
Applies the configuration only when specified classes are present on the classpath.
When using
@ConditionalOnClass on a @Bean method, isolate the condition in a nested @Configuration class. Otherwise the JVM may load and process the method’s return type before the condition is evaluated, causing a ClassNotFoundException at startup.@ConditionalOnMissingBean
@ConditionalOnMissingBean
Applies the configuration only when no bean of the specified type is already registered in the The condition checks only the bean definitions processed so far, so it is strongly recommended to use
BeanFactory. This is what allows you to override auto-configured beans by defining your own.@ConditionalOnMissingBean only within auto-configuration classes.@ConditionalOnProperty
@ConditionalOnProperty
Applies the configuration only when a specified property has a given value (or is simply present and not set to The
false).matchIfMissing attribute controls the outcome when the property is absent from the environment.Gradually replacing auto-configuration
Auto-configuration is non-invasive. The moment you register your own bean of a type that auto-configuration would have provided, the auto-configured bean backs away. For example, if you define your ownDataSource bean, the embedded database auto-configuration no longer applies.
To understand which auto-configurations are currently active and why, start your application with the --debug flag:
Disabling specific auto-configurations
If a particular auto-configuration is being applied and you do not want it, use theexclude attribute of @SpringBootApplication:
excludeName attribute with the fully qualified class name instead:
Even though auto-configuration classes are
public, only the class name is considered public API. The internal contents — nested configuration classes and bean methods — are not meant to be used directly.Writing your own auto-configuration
Create the auto-configuration class
Annotate your class with
@AutoConfiguration instead of @Configuration. This is a composed annotation that sets proxyBeanMethods = false and participates in the auto-configuration ordering mechanism.Register the auto-configuration class
Create a file at Spring Boot loads this file using
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports in your jar and list your auto-configuration class name:ImportCandidates at startup.Auto-configuration packages
The package of the class annotated with@EnableAutoConfiguration (typically your main application class) becomes the default auto-configuration package. Auto-configured features such as JPA entity scanning and Spring Data repository scanning look in this package by default.
You can register additional packages using @AutoConfigurationPackage: