首页
学习
活动
专区
圈层
工具
发布
30 篇文章
1
Spring Boot2 系列教程(二)创建 Spring Boot 项目的三种方式
2
Spring Boot2 系列教程(三)理解 spring-boot-starter-parent
3
Spring Boot2 系列教程(五)Spring Boot中的 yaml 配置
4
Spring Boot2 系列教程(六)自定义 Spring Boot 中的 starter
5
Spring Boot2 系列教程(七)理解自动化配置的原理
6
Spring Boot2 系列教程(八)Spring Boot 中配置 Https
7
Spring Boot2 系列教程(九)Spring Boot 整合 Thymeleaf
8
Spring Boot2 系列教程(十九)Spring Boot 整合 JdbcTemplate
9
Spring Boot2 系列教程(十八)Spring Boot 中自定义 SpringMVC 配置
10
Spring Boot2 系列教程(十七)SpringBoot 整合 Swagger2
11
Spring Boot2 系列教程(十六)定时任务的两种实现方式
12
Spring Boot2 系列教程(十五)定义系统启动任务的两种方式
13
Spring Boot2 系列教程(十三)Spring Boot 中的全局异常处理
14
Spring Boot2 系列教程(十一)Spring Boot 中的静态资源配置
15
Spring Boot2 系列教程(十二)@ControllerAdvice 的三种使用场景
16
Spring Boot2 系列教程(十四)CORS 解决跨域问题
17
Spring Boot2 系列教程(十)Spring Boot 整合 Freemarker
18
Spring Boot2 系列教程(二十八)Spring Boot 整合 Session 共享
19
Spring Boot2 系列教程(二十九)Spring Boot 整合 Redis
20
Spring Boot2 系列教程(二十七)Nginx 极简扫盲入门
21
Spring Boot2 系列教程(二十六)Spring Boot 整合 Redis
22
Spring Boot2 系列教程(二十二)整合 MyBatis 多数据源
23
Spring Boot2 系列教程(二十一)整合 MyBatis
24
Spring Boot2 系列教程(二十)Spring Boot 整合JdbcTemplate 多数据源
25
Spring Boot2 系列教程(三十七)Spring Security 整合 JWT
26
Spring Boot2 系列教程(三十六)SpringSecurity 中角色继承的坑
27
Spring Boot2 系列教程(三十五)SpringSecurity 使用 JSON 格式登录
28
Spring Boot2 系列教程(三十四)Spring Security 添加验证码
29
Spring Boot2 系列教程(三十三)整合 Spring Security
30
Spring Boot2 系列教程(三十一)Spring Boot 构建 RESTful 风格应用

Spring Boot2 系列教程(五)Spring Boot中的 yaml 配置

搞 Spring Boot 的小伙伴都知道,Spring Boot 中的配置文件有两种格式,properties 或者 yaml,一般情况下,两者可以随意使用,选择自己顺手的就行了,那么这两者完全一样吗?肯定不是啦!本文就来和大伙重点介绍下 yaml 配置,最后再来看看 yaml 和 properties 配置有何区别。

狡兔三窟

首先 application.yaml 在 Spring Boot 中可以写在四个不同的位置,分别是如下位置:

  1. 项目根目录下的 config 目录中
  2. 项目根目录下
  3. classpath 下的 config 目录中
  4. classpath 目录下

四个位置中的 application.yaml 文件的优先级按照上面列出的顺序依次降低。即如果有同一个属性在四个文件中都出现了,以优先级高的为准。

那么 application.yaml 是不是必须叫 application.yaml 这个名字呢?当然不是必须的。开发者可以自己定义 yaml 名字,自己定义的话,需要在项目启动时指定配置文件的名字,像下面这样:

当然这是在 IntelliJ IDEA 中直接配置的,如果项目已经打成 jar 包了,则在项目启动时加入如下参数:

代码语言:javascript
复制
java -jar myproject.jar --spring.config.name=app

这样配置之后,在项目启动时,就会按照上面所说的四个位置按顺序去查找一个名为 app.yaml 的文件。当然这四个位置也不是一成不变的,也可以自己定义,有两种方式,一个是使用 spring.config.location 属性,另一个则是使用 spring.config.additional-location 这个属性,在第一个属性中,表示自己重新定义配置文件的位置,项目启动时就按照定义的位置去查找配置文件,这种定义方式会覆盖掉默认的四个位置,也可以使用第二种方式,第二种方式则表示在四个位置的基础上,再添加几个位置,新添加的位置的优先级大于原本的位置。

配置方式如下:

这里要注意,配置文件位置时,值一定要以 / 结尾。

数组注入

yaml 也支持数组注入,例如

代码语言:javascript
复制
my:
  servers:
	- dev.example.com
	- another.example.com

这段数据可以绑定到一个带 Bean 的数组中:

代码语言:javascript
复制
@ConfigurationProperties(prefix="my")
@Component
public class Config {

	private List<String> servers = new ArrayList<String>();

	public List<String> getServers() {
		return this.servers;
	}
}

项目启动后,配置中的数组会自动存储到 servers 集合中。当然,yaml 不仅可以存储这种简单数据,也可以在集合中存储对象。例如下面这种:

代码语言:javascript
复制
redis:
  redisConfigs:
    - host: 192.168.66.128
      port: 6379
    - host: 192.168.66.129
      port: 6380

这个可以被注入到如下类中:

代码语言:javascript
复制
@Component
@ConfigurationProperties(prefix = "redis")
public class RedisCluster {
    private List<SingleRedisConfig> redisConfigs;
	//省略getter/setter
}

优缺点

不同于 properties 文件的无序,yaml 配置是有序的,这一点在有些配置中是非常有用的,例如在 Spring Cloud Zuul 的配置中,当我们配置代理规则时,顺序就显得尤为重要了。当然 yaml 配置也不是万能的,例如,yaml 配置目前不支持 @PropertySource 注解。

下一篇
举报
领券