How to Configure Datasource in Spring Boot Easily
To configure a datasource in Spring Boot, define your database connection properties in
application.properties or application.yml using keys like spring.datasource.url, spring.datasource.username, and spring.datasource.password. Spring Boot auto-configures the datasource bean based on these settings, or you can create a custom @Bean of type DataSource in a configuration class.Syntax
Spring Boot datasource configuration mainly uses properties in application.properties or application.yml. Key properties include:
spring.datasource.url: The JDBC URL of your database.spring.datasource.username: The database username.spring.datasource.password: The database password.spring.datasource.driver-class-name: The JDBC driver class name (optional if Spring Boot can detect it).
You can also define a @Bean method returning a DataSource object for custom setups.
properties
spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=root spring.datasource.password=secret spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
Example
This example shows how to configure a MySQL datasource using application.properties and a simple Spring Boot application that connects to the database.
java
/* application.properties */ spring.datasource.url=jdbc:mysql://localhost:3306/testdb spring.datasource.username=testuser spring.datasource.password=testpass spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver /* MainApplication.java */ package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.beans.factory.annotation.Autowired; import javax.annotation.PostConstruct; @SpringBootApplication public class MainApplication { @Autowired private JdbcTemplate jdbcTemplate; public static void main(String[] args) { SpringApplication.run(MainApplication.class, args); } @PostConstruct public void testDb() { Integer count = jdbcTemplate.queryForObject("SELECT COUNT(*) FROM users", Integer.class); System.out.println("Users count: " + count); } }
Output
Users count: 5
Common Pitfalls
Common mistakes when configuring datasource in Spring Boot include:
- Missing or incorrect JDBC URL format causes connection failures.
- Not specifying the correct driver class name for the database.
- Forgetting to include the database driver dependency in
pom.xmlorbuild.gradle. - Hardcoding credentials in properties without environment-specific overrides.
- Trying to manually create a
DataSourcebean without disabling Spring Boot auto-configuration.
xml
/* Wrong: Missing driver dependency in build file */ // Causes ClassNotFoundException for driver /* Right: Add dependency in pom.xml for MySQL */ // <dependency> // <groupId>mysql</groupId> // <artifactId>mysql-connector-java</artifactId> // <scope>runtime</scope> // </dependency>
Quick Reference
| Property | Description | Example |
|---|---|---|
| spring.datasource.url | JDBC URL of the database | jdbc:mysql://localhost:3306/mydb |
| spring.datasource.username | Database username | root |
| spring.datasource.password | Database password | secret |
| spring.datasource.driver-class-name | JDBC driver class name | com.mysql.cj.jdbc.Driver |
| spring.datasource.hikari.maximum-pool-size | Max connections in pool | 10 |
Key Takeaways
Configure datasource properties in application.properties or application.yml for Spring Boot auto-configuration.
Ensure the correct JDBC URL, username, password, and driver class name are set for your database.
Include the proper database driver dependency in your build file to avoid connection errors.
Use JdbcTemplate or other Spring components to interact with the configured datasource easily.
Avoid manual DataSource bean creation unless you disable Spring Boot's auto-configuration.