Implementation:APIJSON APIJSON Demo DemoDataSourceConfig
| Knowledge Sources | |
|---|---|
| Domains | Configuration, Database, Connection_Pool |
| Last Updated | 2026-10-14 00:00 GMT |
Overview
Concrete tool for configuring multiple Druid connection pool datasources as Spring beans, provided by the APIJSON Demo's Spring Boot configuration.
Description
The DemoDataSourceConfig class is a Spring @Configuration class that registers three DruidDataSource beans, each bound to a different YAML configuration prefix. This enables multi-datasource support where different API requests can target different databases (primary, test, online) via the @datasource keyword.
The three datasources:
- druidDataSource: Bound to
spring.datasource.druid— primary database - druidTestDataSource: Bound to
spring.datasource.druid-test— test database - druidOnlineDataSource: Bound to
spring.datasource.druid-online— online/production database
Each datasource is configured via @ConfigurationProperties, which automatically binds YAML properties (url, username, password, initial-size, max-active, etc.) to the Druid pool.
Usage
- Standard multi-datasource: Configured automatically at startup via Spring Boot auto-configuration.
- Adding a new datasource: Add a new
@Beanmethod with a new@ConfigurationPropertiesprefix. - Switching to HikariCP: Uncomment the HikariCP bean and comment out Druid beans.
Code Reference
Source Location
- Repository: APIJSON-Demo
- File: APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/java/apijson/boot/DemoDataSourceConfig.java
Signature
@Configuration
public class DemoDataSourceConfig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource.druid")
public DruidDataSource druidDataSource() {
return new DruidDataSource();
}
@Bean
@ConfigurationProperties(prefix = "spring.datasource.druid-test")
public DruidDataSource druidTestDataSource() {
return new DruidDataSource();
}
@Bean
@ConfigurationProperties(prefix = "spring.datasource.druid-online")
public DruidDataSource druidOnlineDataSource() {
return new DruidDataSource();
}
}
Import
import apijson.boot.DemoDataSourceConfig;
// Auto-configured by Spring Boot
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| YAML config | Properties | Yes | application.yml datasource configuration
|
Outputs
| Name | Type | Description |
|---|---|---|
| druidDataSource | DruidDataSource | Primary database connection pool bean |
| druidTestDataSource | DruidDataSource | Test database connection pool bean |
| druidOnlineDataSource | DruidDataSource | Online database connection pool bean |
Usage Examples
application.yml Configuration
spring:
datasource:
druid:
url: jdbc:mysql://localhost:3306?serverTimezone=GMT%2B8
username: root
password: 123456
initial-size: 5
max-active: 20
druid-test:
url: jdbc:mysql://test-db:3306?serverTimezone=GMT%2B8
username: test_user
password: test_pass
initial-size: 3
max-active: 10
druid-online:
url: jdbc:mysql://prod-db:3306?serverTimezone=GMT%2B8
username: prod_user
password: prod_pass
initial-size: 10
max-active: 50
Using a Datasource in a Request
{
"User": {
"@datasource": "DRUID-TEST",
"id": 82001
}
}