Joh*_*ick 25 java spring yaml spring-boot
在Spring Boot中,我知道我可以用application.yml替换application.properties并使用YAML格式.但是,我的application.yml越来越拥挤,所以我需要将它分开一点.我怎样才能做到这一点?我想做这样的事情:
...
@Configuration
@EnableAutoConfiguration
@EnableWebMvc
@EnableScheduling
@PropertySource({"classpath:application.yml", "classpath:scheduling.yml"})
public class ApplicationConfig {
...
Run Code Online (Sandbox Code Playgroud)
Mak*_*min 35
@PropertySource注释,你不需要它scheduling.yml成src/main/resources/application-scheduling.ymlsrc/main/resources/application.yml在下一行添加文件:
spring.profiles.include: 'scheduling'
Dav*_*yer 11
@PropertySource不支持YAML(可能会在Spring 4.1中支持).您可以设置spring.config.location或spring.config.name以逗号分隔的列表(例如,作为系统属性或命令行参数).
就个人而言,我喜欢我在同一个地方的所有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
Run Code Online (Sandbox Code Playgroud)
您可以在主yaml文件中使用活动配置文件概念.例如:
spring.profiles.active: test
Run Code Online (Sandbox Code Playgroud)
这意味着你应该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
Run Code Online (Sandbox Code Playgroud)
并且您必须为每个文件设置不同的设置。
您只需要在适当的环境中设置您的设置,例如 dev、uat ot prod 级别,并且只需在application.yml文件中添加一个属性。
spring:
profiles:
active: dev
application: /* optional */
name: Application Name
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
31072 次 |
| 最近记录: |