Spring Boot Tutorial
Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)
Prerequisite (Spring Core Concepts)
Spring Boot Core
Spring Boot with REST API
Spring Boot with Database and Data JPA
Spring Boot with Kafka
Spring Boot with AOP
Spring Boot's auto-configuration is one of its most powerful features, designed to reduce the effort required to set up and configure a Spring application. Auto-configuration attempts to automatically configure the Spring application based on the libraries present in the classpath.
Let's delve into how auto-configuration works and its key aspects:
Check Dependencies: When a Spring Boot application starts, it examines the libraries in the classpath. Based on these libraries, it makes some educated guesses about what you're likely going to need.
Default Configurations: Spring Boot provides a plethora of default configurations, assuming you'd want a ready-to-run application.
Conditional Configuration: It doesn't just blindly apply all default configurations. Instead, Spring Boot checks a variety of conditions before applying a configuration. This is done through the @Conditional
annotations or their variants (@ConditionalOnClass
, @ConditionalOnBean
, @ConditionalOnProperty
, etc.).
Custom Starters: Spring Boot comes with a set of predefined project templates called "Starters". These starters are dependency descriptors that bring in a bunch of related libraries to help achieve a functionality (like web development, JPA-based data access, etc.). When you include a starter in your project, Spring Boot auto-configures components based on the libraries in that starter.
Properties Overriding: Even though Spring Boot provides many default configurations, it also offers the flexibility to override these defaults. You can use application.properties
or application.yml
files to customize these settings.
Minimal Configuration: One of the main goals of auto-configuration is to reduce the need for specifying beans in the configuration file. If Spring Boot's assumptions match your needs, you may not need to write any configuration at all!
If you have spring-boot-starter-web
on your classpath, Spring Boot assumes you're developing a web application and sets up Tomcat as an embedded server.
If you have JPA libraries and a database connection in the classpath, Spring Boot will set up the default configurations for a database, including a DataSource
bean, EntityManager
, and more. If you've defined your DataSource
properties, it will use those.
If spring-boot-starter-data-jpa
is in the classpath but there's no EntityManager
bean defined, Spring Boot will auto-configure one for you. But if you define your custom EntityManager
, Spring Boot will back off and won't create the default one.
If you find that specific auto-configurations aren't useful, you can exclude them. Use the exclude
attribute of @SpringBootApplication
or @EnableAutoConfiguration
:
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) public class MyApplication { // ... }
For debugging purposes or to better understand what auto-configurations have been applied, you can enable the auto-configuration report by setting the property debug=true
in your application.properties
.
Auto-configuration significantly simplifies the setup and configuration process for Spring applications, allowing developers to focus on building the application's core functionality. However, it's always good to understand the underlying configurations and conventions to effectively troubleshoot or optimize when necessary.
How does auto-configuration work in Spring Boot?
// Auto-configured DataSource bean @Configuration(proxyBeanMethods = false) @ConditionalOnClass({ DataSource.class, EmbeddedDatabaseType.class }) @ConditionalOnMissingBean(DataSource.class) @ConditionalOnProperty(name = "spring.datasource.type") class DataSourceConfiguration { @Bean @ConfigurationProperties(prefix = "spring.datasource") DataSource dataSource() { return DataSourceBuilder.create().build(); } }
Customizing auto-configuration in Spring Boot:
// Custom DataSource configuration @Configuration public class CustomDataSourceConfig { @Bean public DataSource customDataSource() { // Custom DataSource bean creation return new CustomDataSource(); } }
Conditional auto-configuration in Spring Boot:
// Conditional DataSource configuration @ConditionalOnProperty(name = "custom.datasource.enabled", havingValue = "true") @Configuration public class ConditionalDataSourceConfig { @Bean public DataSource conditionalDataSource() { // Conditional DataSource bean creation return new ConditionalDataSource(); } }
Ordering and prioritizing auto-configurations in Spring Boot:
@AutoConfigureOrder
or @Order
to specify the order of auto-configurations.// Custom Auto-configuration with order @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE) @Configuration public class CustomAutoConfiguration { // Auto-configuration logic }
Excluding auto-configuration classes in Spring Boot:
@SpringBootApplication
annotation.// Exclude DataSourceAutoConfiguration @SpringBootApplication(exclude = DataSourceAutoConfiguration.class) public class MyApplication { // Application code }
Creating custom auto-configuration in Spring Boot:
@Configuration
and @Conditional
annotations.// Custom Auto-configuration @Configuration @ConditionalOnClass(MyService.class) public class MyAutoConfiguration { @Bean public MyService myService() { // Custom service bean creation return new MyService(); } }
Troubleshooting auto-configuration issues in Spring Boot:
# Enable debug logging for auto-configuration logging: level: org.springframework.boot.autoconfigure: DEBUG