小编bit*_*boy的帖子

如何配置spring-boot以使用基于文件的H2数据库

我已经成功创建了一个在内存中使用H2嵌入式数据库的spring启动应用程序.我现在想将其更改为基于文件的版本,该版本将持续存在.

我尝试过只更改文件中的spring.datasource.*属性application.properties,它们看起来像这样:

spring.datasource.url=jdbc:h2:file:~/test;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username=test
spring.datasource.password=test
spring.datasource.driverClassName=org.h2.Driver`  
Run Code Online (Sandbox Code Playgroud)

看起来像spring boot只是忽略了这些设置,因为它只是如下所示:

o.s.j.d.e.EmbeddedDatabaseFactory        : Starting embedded database: url='jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'
Run Code Online (Sandbox Code Playgroud)

pom.xml包含以下可能与此帖子相关的依赖项:

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.3.5.RELEASE</version>
</parent>
....
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency> 
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

我从文档和一些帖子中了解到,配置应该正常工作,但对我来说没有运气.只是为了防止我尝试过的一些基本错误并检查以下内容:

  1. 我的应用程序属性在classspath中:
  2. 我试图排除注释中的自动配置 @EnableAutoConfiguration
  3. 我试图注入一个dataSource带有注释组合的bean @Primary,@ConfigurationProperties(prefix = "spring.datasource")并以编程方式设置属性DataSourceBuilder.这会导致与类型相关的其他错误null.

似乎我错过了一个关键概念或其他东西.谁能帮忙.

更新1:从我的自动配置报告中提取:

Positive matches:
-----------------

    DataSourceAutoConfiguration matched
  - @ConditionalOnClass classes found: javax.sql.DataSource,org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType (OnClassCondition)

   DataSourceAutoConfiguration.DataSourceInitializerConfiguration matched
  - @ConditionalOnMissingBean (types: org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer; SearchStrategy: all) found no …
Run Code Online (Sandbox Code Playgroud)

h2 spring-boot

58
推荐指数
3
解决办法
6万
查看次数

标签 统计

h2 ×1

spring-boot ×1