This module provides the auto-configuration logic for connecting to and managing SQL and NoSQL databases. It abstracts the complexity of manual DataSource or client bean creation by providing opinionated defaults, property-based configuration, and integration with popular migration tools and Spring Data abstractions.
The core of SQL data access is the DataSourceAutoConfiguration, which facilitates the creation of a javax.sql.DataSource bean.
Spring Boot can auto-configure an embedded database if a supported driver (H2, HSQL, or Derby) is on the classpath and no connection URL is provided. If multiple embedded databases are present, spring.datasource.embedded-database-connection can be used to select one.
For production environments, Spring Boot selects a connection pool based on the following priority:
The DataSourceBuilder uses detection logic to pick the most suitable pooling implementation module/spring-boot-jdbc/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java52-54 It also supports non-pooling types like SimpleDriverDataSource module/spring-boot-jdbc/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java71-72
| Class | Role |
|---|---|
DataSourceAutoConfiguration | Main entry point for JDBC auto-configuration. |
DataSourceBuilder | Fluent API to create a DataSource manually module/spring-boot-jdbc/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java92 |
DataSourceUnwrapper | Utility to unwrap a DataSource to access vendor-specific features module/spring-boot-jdbc/src/main/java/org/springframework/boot/jdbc/DataSourceUnwrapper.java31 |
DataSource Configuration Flow
Sources: module/spring-boot-jdbc/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java52-77 module/spring-boot-hibernate/src/test/java/org/springframework/boot/hibernate/autoconfigure/HibernateJpaAutoConfigurationTests.java78-82
Spring Boot simplifies JPA setup via JpaBaseConfiguration and its Hibernate-specific implementation, HibernateJpaConfiguration.
LocalContainerEntityManagerFactoryBean instances using a fluent API module/spring-boot-jpa/src/main/java/org/springframework/boot/jpa/EntityManagerFactoryBuilder.java61show-sql and database-platform module/spring-boot-jpa/src/main/java/org/springframework/boot/jpa/autoconfigure/JpaProperties.java35By default, Spring Boot configures Hibernate to use SpringImplicitNamingStrategy module/spring-boot-hibernate/src/main/java/org/springframework/boot/hibernate/autoconfigure/HibernateJpaConfiguration.java43 and PhysicalNamingStrategySnakeCaseImpl module/spring-boot-hibernate/src/test/java/org/springframework/boot/hibernate/autoconfigure/HibernateJpaAutoConfigurationTests.java51
Sources: module/spring-boot-jpa/src/main/java/org/springframework/boot/jpa/autoconfigure/JpaBaseConfiguration.java71 module/spring-boot-hibernate/src/main/java/org/springframework/boot/hibernate/autoconfigure/HibernateJpaConfiguration.java71
Auto-configuration is provided for various Spring Data modules. These configurations typically look for repository interfaces in the package of the @EnableAutoConfiguration class.
DataJdbcRepositoriesAutoConfiguration enables repositories when a DataSource, NamedParameterJdbcOperations, and TransactionManager are present module/spring-boot-data-jdbc/src/test/java/org/springframework/boot/data/jdbc/autoconfigure/DataJdbcRepositoriesAutoConfigurationTests.java109-119DataJpaRepositoriesAutoConfiguration handles the setup of JPA repositories module/spring-boot-data-jpa/src/main/java/org/springframework/boot/data/jpa/autoconfigure/DataJpaRepositoriesAutoConfiguration.java48MongoReactiveAutoConfiguration and standard MongoAutoConfiguration provide the MongoClient and MongoTemplate documentation/spring-boot-docs/src/docs/antora/modules/reference/pages/data/nosql.adoc146-148ElasticsearchRestClientAutoConfiguration provides the Rest5Client module/spring-boot-elasticsearch/src/test/java/org/springframework/boot/elasticsearch/autoconfigure/ElasticsearchRestClientAutoConfigurationTests.java76-79Spring Boot supports Lettuce and Jedis clients module/spring-boot-data-redis/src/main/java/org/springframework/boot/data/redis/autoconfigure/JedisConnectionConfiguration.java64 It auto-configures RedisConnectionFactory and handles pooling via GenericObjectPool module/spring-boot-data-redis/src/main/java/org/springframework/boot/data/redis/autoconfigure/JedisConnectionConfiguration.java61
Redis Configuration Entities
Sources: module/spring-boot-data-redis/src/main/java/org/springframework/boot/data/redis/autoconfigure/JedisConnectionConfiguration.java60-64 module/spring-boot-data-redis/src/main/java/org/springframework/boot/data/redis/autoconfigure/DataRedisConnectionConfiguration.java1
Spring Boot provides first-class support for Flyway and Liquibase, ensuring migrations run before the EntityManagerFactory or DataSource is fully utilized.
The FlywayAutoConfiguration triggers migrations using the Flyway bean module/spring-boot-flyway/src/main/java/org/springframework/boot/flyway/autoconfigure/FlywayAutoConfiguration.java147-152 It can use the primary DataSource or a dedicated one if spring.flyway.url is provided module/spring-boot-flyway/src/main/java/org/springframework/boot/flyway/autoconfigure/FlywayAutoConfiguration.java154-155 The FlywayMigrationInitializer ensures execution at the correct point in the lifecycle module/spring-boot-flyway/src/main/java/org/springframework/boot/flyway/autoconfigure/FlywayMigrationInitializer.java32
Properties under spring.liquibase.* control the changelog location and execution behavior. Spring Boot ensures that SpringLiquibase is initialized early in the application lifecycle.
Sources: module/spring-boot-flyway/src/main/java/org/springframework/boot/flyway/autoconfigure/FlywayAutoConfiguration.java105-111 module/spring-boot-flyway/src/main/java/org/springframework/boot/flyway/autoconfigure/FlywayMigrationInitializer.java32-45
The MongoProperties class holds configuration such as host, port, and uri module/spring-boot-mongodb/src/main/java/org/springframework/boot/mongodb/autoconfigure/MongoProperties.java34 For reactive setups, MongoReactiveAutoConfiguration is used module/spring-boot-mongodb/src/main/java/org/springframework/boot/mongodb/autoconfigure/MongoReactiveAutoConfiguration.java49
MongoDB Client Setup
Sources: module/spring-boot-mongodb/src/main/java/org/springframework/boot/mongodb/autoconfigure/MongoProperties.java34 documentation/spring-boot-docs/src/docs/antora/modules/reference/pages/data/nosql.adoc146-150
Auto-configuration for Elasticsearch provides a Rest5Client and a Rest5ClientBuilder module/spring-boot-elasticsearch/src/main/java/org/springframework/boot/elasticsearch/autoconfigure/ElasticsearchRestClientConfigurations.java134-136 It supports a Sniffer for node discovery if spring.elasticsearch.restclient.sniffer.enabled is set module/spring-boot-elasticsearch/src/main/java/org/springframework/boot/elasticsearch/autoconfigure/ElasticsearchRestClientConfigurations.java142-143
Spring Boot uses the ConnectionDetails abstraction (e.g., ElasticsearchConnectionDetails, MongoConnectionDetails) to allow external configuration sources, such as Testcontainers or Docker Compose, to override standard properties module/spring-boot-elasticsearch/src/main/java/org/springframework/boot/elasticsearch/autoconfigure/ElasticsearchConnectionDetails.java31
Sources: module/spring-boot-elasticsearch/src/main/java/org/springframework/boot/elasticsearch/autoconfigure/ElasticsearchRestClientConfigurations.java89-92 test-support/spring-boot-docker-test-support/src/main/java/org/springframework/boot/testsupport/container/TestImage.java114-117
Refresh this wiki