我有一个带有两个属性文件的Spring Boot服务器:application-local.properties
和application-test.properties
每个文件都包含开发环境和测试环境的配置。可以这样启动:
-Dspring.profiles.active=local
但是在新的Spring Boot项目中,我使用 .yaml
配置文件。我不明白如何使用 profiles
和 .yaml
。我尝试阅读文档,但一无所获。你能否逐步解释一下该怎么做?
我需要两个文件吗?
application-local.yaml
和 application-test.yaml
还是需要写在一个application.yaml
文件中?如果是一个文件,如何分隔配置项?这是我的配置:
server:
path: ***
port: ***
cxf:
path: ***
spring.datasource:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: oracle.jdbc.OracleDriver
url: ***
username: ***
password: ***
hikari:
minimumIdle: 5
maximumPoolSize: 20
idleTimeout: 30000
poolName: SpringBootJPAHikariCP
maxLifetime: 2000000
connectionTimeout: 30000
connection-test-query: SELECT 1 FROM DUAL
spring.jpa:
show-sql: false
database-platform: org.hibernate.dialect.Oracle10gDialect
properties.hibernate.jdbc.batch_size: 30
properties.hibernate.cache.use_second_level_cache: false
hibernate:
ddl-auto: validate
spring.cache:
ehcache:
config: classpath:ehcache.xml
#app configs
my:
messages-max-count: 5
messages-delay: 100
schedulers-charge-delay: 100
client:
first-server-address: ***
second-server-address: ***
last-server-address: ***
enabled-client: FirstClient
我想创建一个测试档案并更改数据库的URL(或更改为PostgreSQL),更改maximumPoolSize
属性。
application-{PROFILE}.yaml
中覆盖了属性,我需要从基础的application.yaml
中删除这个属性吗? - ip696bootRun
是否支持D
选项,你需要使用java -jar -Dspring.profiles.active=test app.jar
。 - Andrew Tobilkospring.profiles.active: test
(使用正确的格式)添加到application.yaml
中,在运行时不要传递任何内容。 - Andrew Tobilko