Analysis and Solutions for Data Source Configuration Issues After Spring Boot 2.0 Migration

Nov 23, 2025 · Programming · 13 views · 7.8

Keywords: Spring Boot 2.0 | DataSource Configuration | HikariCP

Abstract: This article provides an in-depth analysis of the jdbcUrl requirement error encountered after upgrading from Spring Boot 1.5.x to 2.0.0, explores configuration changes due to HikariCP as the default connection pool, and offers multiple solutions including removing custom dataSource methods, using jdbc-url properties, and manual DataSource configuration to facilitate a smooth migration.

Problem Background and Error Analysis

After migrating from Spring Boot 1.5.x to 2.0.0, many developers face application startup failures with the error message: "dataSource or dataSourceClassName or jdbcUrl is required." This issue arises because Spring Boot 2.0 switches the default JDBC connection pool from Tomcat to HikariCP, which has specific requirements for data source configuration properties.

Root Cause: Configuration Differences in HikariCP

HikariCP, as a high-performance connection pool, uses different configuration properties compared to the Tomcat pool. A key difference is that HikariCP requires the jdbcUrl property instead of url to specify the database connection string. In Spring Boot 2.0, if the application relies on auto-configuration but the configuration file still uses spring.datasource.url, HikariCP fails to recognize it, leading to data source initialization errors.

Solution 1: Remove Custom DataSource Configuration

As mentioned in the problem description, the simplest solution is to remove the custom dataSource() method. In Spring Boot 2.0, the auto-configuration mechanism is robust enough to create the data source automatically based on settings in application.yml or application.properties. For example, remove the following code from DatabaseConfig.java:

@Bean
@Primary
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
    return DataSourceBuilder.create().build();
}

After removal, Spring Boot will automatically use HikariCP and parse the configuration correctly, allowing the application to start successfully.

Solution 2: Use the jdbc-url Property

If custom configuration must be retained, change the url property to jdbc-url. Modify application.yml as follows:

spring:
  datasource:
    driverClassName: org.postgresql.Driver
    jdbc-url: jdbc:postgresql://localhost:5432/test
    username: test
    password: 1234

This change ensures that HikariCP can read the database connection information properly.

Solution 3: Manual DataSource Configuration

For scenarios requiring multiple data sources or finer control, manually create the DataSource. Referring to Answer 3, use DriverManagerDataSource or HikariDataSource:

@Autowired
Environment env;

@Primary
@Bean
public DataSource customDataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(env.getProperty("custom.datasource.driver-class-name"));
    dataSource.setUrl(env.getProperty("custom.datasource.url"));
    dataSource.setUsername(env.getProperty("custom.datasource.username"));
    dataSource.setPassword(env.getProperty("custom.datasource.password"));
    return dataSource;
}

This approach avoids compatibility issues with DataSourceBuilder by directly setting properties, ensuring correct data source initialization.

Configuration Property Comparison and Best Practices

In Spring Boot 2.0, it is recommended to use spring.datasource.jdbc-url instead of spring.datasource.url. For multiple data sources, define independent property prefixes, such as app.datasource.primary.jdbc-url, and bind them in configuration classes using @ConfigurationProperties. Additionally, ensure that HikariCP is included in dependencies; Spring Boot 2.0 provides it by default, so no explicit addition is needed.

Summary and Migration Recommendations

Data source configuration is a common issue during Spring Boot 2.0 migration. By removing redundant custom configurations, updating property names, or manually creating data sources, startup errors can be resolved quickly. It is advisable to read official documentation before migration and use the spring-boot-properties-migrator module to automatically detect configuration changes, minimizing compatibility problems.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.