首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >弹簧轮廓没有加载一些属性吗?

问弹簧轮廓没有加载一些属性吗?
EN

Stack Overflow用户
提问于 2021-02-18 20:20:19
回答 1查看 134关注 0票数 0

我想要两个配置文件,一个与MySQL数据源,另一个与H2数据源。所以我创建了

代码语言:javascript
复制
 application.properties:

 spring.jpa.hibernate.ddl-auto=create-drop
 spring.jpa.show-sql=true
 spring.datasource.url=jdbc:mysql://localhost:3306/boilerplate_springboot_restapi
 spring.datasource.username=root
 spring.datasource.password=password
 spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect

和

代码语言:javascript
复制
 application-test.properties:

 spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
 spring.h2.console.enabled=true
 spring.datasource.url=jdbc:h2:mem:testdb
 spring.datasource.driverClassName=org.h2.Driver
 spring.datasource.username=admin
 spring.datasource.password=admin

我使用选项-Dspring.profiles.active=test来运行带有测试配置文件的应用程序。

有趣的是,测试概要文件似乎选择了H2数据源,但没有选择方言,所以它崩溃了。如果我将H2配置放在application.properties中,它就能正常工作。

下面是运行测试配置文件的日志:

代码语言:javascript
复制
[  restartedMain] c.b.r.SpringBoot2RestServiceApplication  : The following profiles are active: test
...
[  restartedMain] o.s.b.a.h2.H2ConsoleAutoConfiguration    : H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:testdb'
[         task-1] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
[         task-1] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.17.Final
[  restartedMain] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
[         task-1] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
[         task-1] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
...
Hibernate: drop table if exists book
Hibernate: drop table if exists hibernate_sequence
Hibernate: create table book....
[         task-1] o.h.t.s.i.ExceptionHandlerLoggedImpl     : GenerationTarget encountered exception accepting command : Error executing DDL "create table book

我认为错误的hibernate方言是崩溃的根本原因。但我不明白我做错了什么。

更新:如果我在application.properties中设置了H2Dialect,它会正确地拾取它...就像主文件中的设置优先于配置文件属性文件中的设置...

更新:我在代码和pom.xml中都没有任何@Profile

更新:我创建了两个属性文件,一个用于MySQL,另一个用于h2,并将所有属性移动到那里。现在,它似乎起作用了。

我发现这个Spring MVC application.properties not override by profile file application-dev.properties也有类似的问题

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-18 22:27:49

您的生产配置使用

代码语言:javascript
复制
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect

而您的测试配置使用

代码语言:javascript
复制
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

当两者都存在时,第一个将取胜(因为属性对象被设置为最后一个)。因此,请在两个文件中使用相同的名称。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66259844

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档