1

版本:

Spring: 5.2.16.RELEASE
Spring Integrations: 5.3.9.RELEASE
PostgreSQL: 13.x

我已经实现了一个纯 Spring 5.x webapp;没有 Spring Boot。

我正在使用JdbcMetadataStore并要求使用位于类路径中的模式定义初始化 PostgreSQL 数据库:

classpath:org/springframework/integration/jdbc/schema-postgresql.sql

在关于该主题的一篇非常有用的文章之后,以下是我放入的属性spring.properties

spring.integration.jdbc.initialize-schema=always
spring.integration.jdbc.schema=classpath:org/springframework/integration/jdbc/schema-postgresql.sql

这不起作用。经过研究这个问题,我了解到启动初始化支持的,但是在 Spring Boot 中。

问题:没有在 webapp 初始化期间在其他地方显式处理此 SQL 脚本的执行,是否有任何标准方法可以在启动时加载上面列出的脚本?注意:我没有使用schema.sql脚本或类似的东西来初始化我的后端。

4

1 回答 1

2

见:DataSourceInitializer_spring-jdbc

/**
 * Used to {@linkplain #setDatabasePopulator set up} a database during
 * initialization and {@link #setDatabaseCleaner clean up} a database during
 * destruction.

 * @see DatabasePopulator
 */
public class DataSourceInitializer implements InitializingBean, DisposableBean {

您需要ResourceDatabasePopulator根据该脚本位置在那里注入:

/**
 * Populates, initializes, or cleans up a database using SQL scripts defined in
 * external resources.
 *
 * <ul>
 * <li>Call {@link #addScript} to add a single SQL script location.
 * <li>Call {@link #addScripts} to add multiple SQL script locations.
 * <li>Consult the setter methods in this class for further configuration options.
 * <li>Call {@link #populate} or {@link #execute} to initialize or clean up the
 * database using the configured scripts.
 * </ul>
 *

 * @see DatabasePopulatorUtils
 * @see ScriptUtils
 */
public class ResourceDatabasePopulator implements DatabasePopulator {

一些文档在这里:https ://docs.spring.io/spring-framework/docs/current/reference/html/data-access.html#jdbc-initializing-datasource

于 2021-11-02T18:24:25.633 回答