311 字
2 分钟
SpringBoot配置文件
AI 摘要
SpringBoot配置文件
定义
SpringBoot项目支持两种配置文件格式:
| 格式 | 文件名 | 特点 |
|---|---|---|
| properties | application.properties | key=value 形式,层级不清晰 |
| yml | application.yml / application.yaml | 以数据为中心,层级清晰,推荐使用 |
语法
YML基本语法
- 大小写敏感
- 数值前必须有空格作为分隔符
- 使用缩进表示层级关系,不允许使用Tab键,只能用空格(IDEA中自动将Tab转换为空格)
- 相同层级的元素左侧对齐即可
#表示注释
定义对象/Map集合
user: name: zhangsan age: 18 password: 123456定义数组/List/Set集合
hobby: - java - game - sport常见场景
数据库连接配置对比
properties 写法:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/web01spring.datasource.username=rootspring.datasource.password=root@1234mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImplyml 写法:
#数据源配置spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/web01 username: root password: root@1234#mybatis配置mybatis: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl注意事项
- 在项目开发中,推荐使用
application.yml配置文件,简洁、明了。 - 如果配置项的值以
0开头,值需要使用单引号引起来,因为以0开头在yml中表示八进制数据。 application.properties和application.yml可共存,优先级由SpringBoot加载顺序决定。- 切换配置文件格式时,将原文件重命名(如改为
_application.properties)即可避免冲突。
相关链接
支持与分享
如果这篇文章对你有帮助,欢迎分享给更多人或赞助支持!
SpringBoot配置文件
https://blog.tsh520.cn/posts/javawebai/后端基础/springboot配置文件/ 最后更新于 2026-05-19,距今已过 32 天
部分内容可能已过时
评论区
[ 标签 ]
[ 分类 ]
[ 公告 ]
如果你喜欢,那么欢迎来到我的世界!
了解更多[ 音乐 ]
找不到相关结果。
[ contents ]
[ 全部文章 ]