Spring Boot:如何使用多个yml文件
在Spring Boot中,我知道我可以用application.yml替换application.properties并使用YAML格式。 但是,我的application.yml越来越拥挤,所以我需要把它分开一点。 我怎样才能做到这一点? 我想做这样的事情:
... @Configuration @EnableAutoConfiguration @EnableWebMvc @EnableScheduling @PropertySource({"classpath:application.yml", "classpath:scheduling.yml"}) public class ApplicationConfig { ...
@PropertySource
不支持YAML(可能会在Spring 4.1中使用)。 您可以将spring.config.location
或spring.config.name
设置为以逗号分隔的列表(例如,作为System属性或命令行参数)。
就个人而言,我喜欢我在同一个地方的所有YAML(结构真的有助于在视觉上分解它,你可以使用文件中的文件将其拆分更多)。 我猜这只是味道。
- 删除
@PropertySource
注释,你不需要它 - 将
scheduling.yml
重命名为src/main/resources/application-scheduling.yml
-
在下一行添加
src/main/resources/application.yml
文件:spring.profiles.include: 'scheduling'
您可以在主yaml文件中使用活动配置文件概念。 例如:
spring.profiles.active: test
这意味着你应该在资源目录中有application-test.yml
文件。 请考虑活动配置文件将覆盖主yaml文件中具有相同名称的属性。
有关更多信息,请访问: http : //docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html
如果我有很多配置和/或环境,通常我会这样做:
$ 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