【springboot】【二】 properties 与 yml 配置文件

上一篇文章中,我们用idea创建了一个springboot项目,idea默认给我们生成的配置文件是.properties文件,但是官方推荐的是.yml 格式的。

我们先来看一看properties文件怎么写的

【springboot】【二】 properties 与 yml 配置文件


改成yml:冒号后面需要空格!!!

【springboot】【二】 properties 与 yml 配置文件

启动一下:

【springboot】【二】 properties 与 yml 配置文件

报错了,因为我们配置文件中缺少了空格,修改过了重新启动:

【springboot】【二】 properties 与 yml 配置文件

可以看到启动成功。

接下来继续说一下配置文件的使用

在项目里使用配置文件中的内容:首先修改一下配置文件,添加几个参数(配置文件中不需要设置参数类型)

【springboot】【二】 properties 与 yml 配置文件

然后看看如何在controller中获取它们:

【springboot】【二】 properties 与 yml 配置文件

访问一下:

【springboot】【二】 properties 与 yml 配置文件

获取成功。