Auto-configuration
Configuration based on what is already in the project
Spring Boot tries to create typical infrastructure automatically when it sees that the application needs it. The decision is based on conditions: which classes are on the classpath, which properties are set, and which beans have already been registered by the user.
For example, if the project contains libraries for a particular technology, Boot may prepare related beans. If the developer creates a custom bean for the same purpose, automatic configuration will often back off and leave control to the user configuration.
Automatic configuration does not mean hidden magic
Auto-configuration is implemented with regular Spring configuration classes and conditions. In simplified form, the idea can be represented like this:
@Configuration
@ConditionalOnClass(MailSender.class)
class MailAutoConfiguration {
@Bean
@ConditionalOnMissingBean
MailService mailService() {
return new MailService();
}
}
Such configuration is applied only when its conditions are satisfied.
@ConditionalOnClass checks whether a class is present,
while @ConditionalOnMissingBean avoids creating a bean
when the application has already defined a suitable object.