[英]Spring Boot: how to use multiple yml files
在Spring Boot中,我知道可以将application.properties替换为application.yml并使用YAML格式。 但是,我的application.yml变得很拥挤,因此我需要对其进行拆分。 我怎样才能做到这一点? 我想做这样的事情:
...
@Configuration
@EnableAutoConfiguration
@EnableWebMvc
@EnableScheduling
@PropertySource({"classpath:application.yml", "classpath:scheduling.yml"})
public class ApplicationConfig {
...
@PropertySource
批注,您不需要它 scheduling.yml
重命名为src/main/resources/application-scheduling.yml
在下一行添加src/main/resources/application.yml
文件:
spring.profiles.include: 'scheduling'
@PropertySource
不支持YAML(可能在Spring 4.1中会支持)。 您可以将spring.config.location
或spring.config.name
设置为逗号分隔的列表(例如,作为System属性或命令行参数)。
就我个人而言,我喜欢所有YAML放在同一位置(该结构确实有助于从视觉上分解它,并且您可以使用文件中的文档来对其进行拆分)。 我想那只是味道。
如果我有很多配置和/或环境,通常我会这样做:
$ cat src/main/resources/application.yml:
spring:
profiles:
include: >
profile1,
profile2,
...
profileN
$ ls -lah src/main/resources/config:
drwxr-xr-x 4 mak staff 136B Apr 16 23:58 .
drwxr-xr-x 6 mak staff 204B Apr 17 01:54 ..
-rw-r--r-- 1 mak staff 60B Apr 16 23:58 application-profile1.yml
-rw-r--r-- 1 mak staff 62B Apr 16 23:16 application-profile2.yml
...
-rw-r--r-- 1 mak staff 50B Apr 16 23:16 application-profileN.yml
您可以在主Yaml文件中使用活动配置文件概念。 例如:
spring.profiles.active: test
这意味着您应该在资源目录中有application-test.yml
文件。 考虑到活动配置文件将覆盖主yaml文件中具有相同名称的属性。
有关更多信息,请访问: http : //docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html
假设您的应用程序需要4个.yml文件。
application.yml
application-dev.yml
application-uat.yml
application-prod.yml
并且您必须为每个文件设置不同的设置。
您只需要在适当的环境(例如dev,uat或prod级别)上进行设置,并且只需要在application.yml
文件中添加一个属性即可。
spring:
profiles:
active: dev
application: /* optional */
name: Application Name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.