我想要两个配置文件,一个与MySQL数据源,另一个与H2数据源。所以我创建了
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和
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中,它就能正常工作。
下面是运行测试配置文件的日志:
[ 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也有类似的问题
发布于 2021-02-18 22:27:49
您的生产配置使用
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect而您的测试配置使用
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect当两者都存在时,第一个将取胜(因为属性对象被设置为最后一个)。因此,请在两个文件中使用相同的名称。
https://stackoverflow.com/questions/66259844
复制相似问题