带有 application.properties 的 spring-boot 数据源配置文件 [英] spring-boot datasource profiles w/ application.properties

查看:63
本文介绍了带有 application.properties 的 spring-boot 数据源配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据反馈更新问题:

我有一个 spring-boot 应用程序,它有三个数据库:H2 用于集成测试,Postgresql 用于 qa &生产.由于 spring-boot 为您创建了默认数据源,因此我没有为集成测试定义任何内容.我以为我会使用 application.properties 来定义我的数据源连接值,但我不确定处理这个问题的最佳方法是什么.

I have a spring-boot application that has three databases: H2 for integration testing, and Postgresql for qa & production. Since spring-boot creates a default datasource for you, I don't have anything defined for my integration tests. I thought I would use application.properties to define my datasource connection values but I am not certain what is the best way to handle this.

我有两个文件:

src/main/resources/application.properties

spring.profiles.active=production
appName = myProduct
serverPort=9001

spring.datasource.url=jdbc:postgresql://localhost/myDatabase
spring.datasource.username=user
spring.datasource.password=password
spring.datasource.driverClassName=org.postgresql.Driver

spring.jpa.hibernate.hbm2ddl.auto=update
spring.jpa.hibernate.ejb.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy
spring.jpa.hibernate.show_sql=true
spring.jpa.hibernate.format_sql=true
spring.jpa.hibernate.use_sql_comments=false
spring.jpa.hibernate.type=all
spring.jpa.hibernate.disableConnectionTracking=true
spring.jpa.hibernate.default_schema=dental

src/main/resources/application-test.properties

spring.profiles.active=test
serverPort=9002

spring.datasource.url = jdbc:h2:~/testdb
spring.datasource.username = sa
spring.datasource.password = 
spring.datasource.driverClassName = org.h2.Driver

liquibase.changeLog=classpath:/db/changelog/db.changelog-master.sql

我曾经使用 gradle(使用gradle build test")或在 IntelliJ 中运行我的测试.我更新了我的 gradle 文件以使用:

I used to run my tests with with gradle (using "gradle build test") or within IntelliJ. I updated my gradle file to use:

task setTestEnv {
    run { systemProperty "spring.profiles.active", "test" }
}

但是当我运行 gradle clean build setTestEnv test 时,我收到似乎表明测试正在尝试连接到实际 Postgresql 数据库的错误:

But when I run gradle clean build setTestEnv test I get errors that seem to indicate the test is trying to connect to an actual Postgresql database:

Caused by: org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:138)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:66)
    at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:125)
    at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:30)
    at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:22)
    at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:32)
    at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:24)
    at org.postgresql.Driver.makeConnection(Driver.java:393)
    at org.postgresql.Driver.connect(Driver.java:267)
    at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:278)
    at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:182)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:701)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:635)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:486)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:144)
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:116)
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:103)
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:127)
    at liquibase.integration.spring.SpringLiquibase.afterPropertiesSet(SpringLiquibase.java:288)
    ... 42 more
Caused by: java.net.ConnectException: Connection refused
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)

我还没有弄清楚如何在 IntelliJ 中设置默认的 system.property == "test"...

I haven't figured out how to set the default system.property == "test" within IntelliJ yet...

推荐答案

21.3 的 Spring Boot 文档.本节介绍如何定义使用格式 application-{profile}.properties 的配置文件特定属性文件.这可以帮助您在每个配置文件的基础上隔离属性.

See section 21.3 of the Spring Boot documentation. This section describes how to define profile specific property files that use the format application-{profile}.properties. This can help you isolate properties on a per profile basis.

这篇关于带有 application.properties 的 spring-boot 数据源配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆