一尘不染

具有application.properties的spring-boot数据源配置文件

spring-boot

根据反馈更新了问题:

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

我有两个文件:

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文件以使用:

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

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

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”。


阅读 399

收藏
2020-05-30

共1个答案

一尘不染

请参阅Spring Boot文档的21.3节。本节描述如何定义使用格式 application- {profile} .properties的
特定于配置文件的属性文件。这可以帮助您根据每个配置文件隔离属性。

2020-05-30