设置名称 Spring 启动嵌入式数据库

Set name of Spring Boot embedded database

如何在测试中设置在 Spring 引导应用程序 运行 中启动的嵌入式数据库的名称?

我正在启动同一个 Spring 启动应用程序的两个实例作为测试的一部分,因为它们是协作的。它们都正确启动了一个 HSQL 数据库,但默认为 testdb 的数据库名称,尽管为 spring.datasource.name.

提供了不同的值

如何提供不同的数据库名称,或其他隔离这两个数据库的方法? 'lightest touch' 会是什么?如果我可以避免它,我宁愿使用属性来控制它,也不愿将 bean 添加到我的测试配置中 - 测试配置 类 不应该因为这个粗粒度的协作测试而变得混乱。

你可以这样试试:

spring.datasource1.name=testdb
spring.datasource2.name=otherdb

然后像这样在您的 ApplicationConfig 中声明数据源

@Bean
@ConfigurationProperties(prefix="spring.datasource1")
public DataSource dataSource1() {
    ...
}

@Bean
@ConfigurationProperties(prefix="spring.datasource2")
public DataSource dataSource2() {
    ...
}

查看官方文档了解更多详情:https://docs.spring.io/spring-boot/docs/current/reference/html/howto-data-access.html#howto-configure-a-datasource

Gah - 设置 spring.datasource.name 更改数据源的名称,但不更改数据库的名称。

设置 spring.datasource.url=jdbc:hsql:mem:mydbname 完全符合我的需要。我必须对嵌入式数据库实现进行硬编码,这有点废话,但是 Spring Boot 使用枚举作为默认值,如果要尝试从 属性 获取名称,这将意味着更大的重写.