【问题标题】:Configure h2 for spring boot为spring boot配置h2
【发布时间】:2015-01-30 10:46:47
【问题描述】:

我正在尝试配置 spring boot 以将我的测试数据源设置为在 postgresql 模式下使用 h2。 我在我的测试/资源/应用程序中设置了这些行:

spring.datasource.url=jdbc:h2:mem:db1;MODE=PostgreSQL
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=

但是 spring boot 一直在加载我的默认 h2 配置。

如何强制 spring boot 使用我的特殊 h2 配置?

【问题讨论】:

  • 文件真的是test/resources/application吗?应该是src/test/resources/application.properties
  • 上述配置开箱即用。您面临什么问题?

标签: spring-boot h2


【解决方案1】:

只需在 java-configuration 中这样做:

    @Configuration
@EnableAutoConfiguration
@Profile({ "dev", "demo" })
public class EmbeddedDatabaseConfiguration {
    @Bean(name = "dataSource")
    public DriverManagerDataSource getDataSource() {
        DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
        driverManagerDataSource.setDriverClassName("org.h2.Driver");
        driverManagerDataSource.setUrl("jdbc:h2:mem:mylivedata;IGNORECASE=TRUE;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1");
        return driverManagerDataSource;
    }
}

【讨论】:

    猜你喜欢
    • 2020-12-22
    • 2016-05-15
    • 1970-01-01
    • 2020-02-26
    • 2019-10-21
    • 1970-01-01
    • 2019-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多