Master Spring Boot 2.5.0 DataSource Initialization: New Configurations Explained
This article walks through Spring Boot 2.5.0's redesigned DataSource script initialization, clarifies deprecated properties, introduces the new SqlInitializationProperties class, and provides a complete example with Maven dependencies, configuration settings, SQL scripts, and best‑practice recommendations for automated database setup.
Spring Boot 2.5.0 introduced a redesign of the DataSource script initialization mechanism. This article explains the deprecated properties, the new configuration class, and provides a step‑by‑step example.
Deprecated Content
The previous version exposed several properties in
org.springframework.boot.autoconfigure.jdbc.DataSourcePropertiesthat are now deprecated, such as initializationMode, platform, schema, schemaUsername, schemaPassword, data, dataUsername, dataPassword, continueOnError, separator, and sqlScriptEncoding.
/**
* Mode to apply when determining if DataSource initialization should be performed
* using the available DDL and DML scripts.
*/
@Deprecated
private DataSourceInitializationMode initializationMode = DataSourceInitializationMode.EMBEDDED;
/**
* Platform to use in the DDL or DML scripts (such as schema-${platform}.sql or
* data-${platform}.sql).
*/
@Deprecated
private String platform = "all";
/** Schema (DDL) script resource references. */
private List<String> schema;
/** Username of the database to execute DDL scripts (if different). */
@Deprecated
private String schemaUsername;
/** Password of the database to execute DDL scripts (if different). */
@Deprecated
private String schemaPassword;
/** Data (DML) script resource references. */
@Deprecated
private List<String> data;
/** Username of the database to execute DML scripts (if different). */
@Deprecated
private String dataUsername;
/** Password of the database to execute DML scripts (if different). */
@Deprecated
private String dataPassword;
/** Whether to stop if an error occurs while initializing the database. */
@Deprecated
private boolean continueOnError = false;
/** Statement separator in SQL initialization scripts. */
@Deprecated
private String separator = ";";
/** SQL scripts encoding. */
@Deprecated
private Charset sqlScriptEncoding;Corresponding properties in application.properties include:
spring.datasource.schema=
spring.datasource.schema-username=
spring.datasource.schema-password=
...New Design
From Spring Boot 2.5.0 onward, the initialization settings are defined in
org.springframework.boot.autoconfigure.sql.init.SqlInitializationProperties. The new properties are prefixed with spring.sql.init..
Example steps:
Create a basic Spring Boot project and add the following Maven dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>Add datasource and initialization settings to application.properties:
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# Spring Boot 2.5.0 init schema & data
spring.sql.init.username=root
spring.sql.init.password=
spring.sql.init.schema-locations=classpath*:schema-all.sqlCreate schema-all.sql under src/main/resources with table definitions, e.g.:
create table test.user_info (
id int unsigned auto_increment comment '用户id' primary key,
open_id varchar(255) default '' null comment '微信小程序openid',
nick_name varchar(255) default '' null comment '微信名',
head_img varchar(255) default '' null comment '微信头像',
sex varchar(255) default '' null comment '性别',
phone varchar(255) default '' null comment '手机',
province varchar(255) default '' null comment '注册地址:省',
city varchar(255) default '' null comment '注册地址:城市',
country varchar(255) default '' null comment '注册地址:县/区',
status tinyint unsigned default 0 not null comment '是否标记删除 0:否 1:是',
create_time datetime not null comment '创建时间',
update_time datetime not null comment '更新时间'
) comment '用户表';Run the application; the user_info table will be created in the test database.
This mechanism automates schema and data initialization during application startup, reducing manual steps.
Configuration Details
spring.sql.init.enabled: Switch to enable/disable initialization (default true). spring.sql.init.username and spring.sql.init.password: Credentials used to run the scripts, allowing separation from the main datasource user. spring.sql.init.schema-locations: Locations of schema scripts (multiple paths separated by ;). spring.sql.init.data-locations: Locations of data scripts (multiple paths separated by ;). spring.sql.init.encoding: Encoding of script files. spring.sql.init.separator: Statement separator (default ;). spring.sql.init.continue-on-error: Whether to continue when a script error occurs (default false).
Application Advice
Use the built‑in initializer mainly for unit tests where the schema is created and dropped automatically.
For production deployments, manage database versioning with Flyway (or Liquibase) and run migrations separately.
Combine Flyway with
org.springframework.jdbc.datasource.init.DataSourceInitializerfor more complex initialization logic.
For the full source code, see the chapter3-13 directory in the GitHub repository https://github.com/dyc87112/SpringBoot-Learning/ or the Gitee mirror.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
