Spring Boot数据源配置文件application.properties中的profiles是什么意思?

15

根据反馈更新的问题:

我有一个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 test时,出现了错误,这些错误似乎表明测试正在尝试连接到实际的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"。


4
为什么不让Spring Boot来配置你的数据源呢?简单地说,将生产环境的数据库设置放在application.properties文件中,然后为测试创建一个名为application-test.properties的文件,包含测试数据库的设置(这将覆盖主要的设置)。或者,如果你只是从maven/gradle运行测试,那么可以在src/test/resources目录下添加一个application.properties文件,覆盖主要的application.properties文件。 - M. Deinum
好的,如果我有一个 src/main/resources/application.properties(包含所有设置,包括生产数据库)和一个 src/main/resources/application-test.properties(只包含 H2 数据库设置)。当我运行我的测试(无论是从命令行还是在 IntelliJ 中)时,我只需要传递一个变量指示“test”,例如 --profile=test?然后当我运行我的应用程序时,将使用 application.properties。 - sonoerin
你不需要自己配置数据源,只需删除它们并在所选的application.properties文件中配置spring.datasource.*属性即可。Spring Boot会负责构建数据源。 - M. Deinum
我更新了问题以展示我的当前状态。 - sonoerin
你使用 db.* 作为属性,这让我觉得你正在自己配置数据源。如前所述,请移除你的数据源并只使用适当的 spring.datasource.* 属性,这将让 Spring Boot 创建数据源。确保覆盖所有属性(对于 H2 数据库,密码可能应该为空)。 - M. Deinum
显示剩余6条评论
2个回答

17

请查看Spring Boot文档的第21.3节。该节描述了如何定义使用格式application-{profile}.properties的特定配置文件,以帮助您按照每个配置文件分类设置属性。


在覆盖配置文件中,我们需要重复属性吗?我的意思是,我们可以有默认属性,然后单独为覆盖设置一个属性,而不必重复每个属性值吗? - Kalpesh Soni
是的。考虑自己进行测试。添加一个application.properties文件和一个特定于配置文件的文件。 - Newbie

10
您可以通过以下方式在测试中添加@ActiveProfiles注释:
@SpringApplicationConfiguration(classes = {Application.class, TestSpringConfiguration.class})
@Test(groups = "integration")
@ActiveProfiles("test")
public class MyServiceTest extends AbstractTransactionalTestNGSpringContextTests {
...
   @Test 
   public void testSomething() {
      ...
   }
} 

我使用TestNG,但JUnit也不会有很大的区别。 你可以按照上面的示例指定额外的配置。

这样就不需要在build.gradle中设置配置文件或在IntelliJ中启动配置文件。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接