Spring配置文件

分钟
Spring配置文件
AI 概括

点击按钮,AI 将为你生成这篇文章的摘要

定义

SpringBoot项目支持两种配置文件格式:

格式文件名特点
propertiesapplication.propertieskey=value 形式,层级不清晰
ymlapplication.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.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/web01
spring.datasource.username=root
spring.datasource.password=root@1234
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

yml 写法

spring:
application:
name: tlias-web-management
datasource:
url: jdbc:mysql://localhost:3306/tlias
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
#上传到阿里云
servlet:
multipart:
max-file-size: 10MB
max-request-size: 100MB
jackson:
date-format: yyyy-MM-dd HH:mm:ss
serialization:
write-dates-as-timestamps: false
time-zone: Asia/Shanghai
mybatis:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true
logging:
level:
org.springframework.web.servlet.mvc.method.annotation: DEBUG
server:
port: 8080
#配置事务管理日志级别
logging:
level:
org.springframework.jdbc.support.JdbcTransactionManager: debug
#配置oss存储
aliyun:
oss:
endpoint: http://oss-cn-beijing.aliyuncs.com
bucketName: java-ai-tlias2026
region: cn-beijing

注意事项

  • 在项目开发中,推荐使用 application.yml 配置文件,简洁、明了。

  • 如果配置项的值以 0 开头,值需要使用单引号引起来,因为以 0 开头在yml中表示八进制数据。

  • application.propertiesapplication.yml 可共存,优先级由SpringBoot加载顺序决定。

  • 切换配置文件格式时,将原文件重命名(如改为 _application.properties)即可避免冲

支持与分享

如果这篇文章对你有帮助,欢迎分享给更多人或赞助支持!

赞助
Spring配置文件
https://f3f3.top/posts/47257415/
作者
lyf
发布于
2026-06-14
许可协议
CC BY-NC-SA 4.0

评论区

Profile Image of the Author
lyf
Hello, I'm LyF.
公告
欢迎来到一飞的博客!。
音乐
封面

音乐

暂未播放

0:00 0:00
暂无歌词
我和宝宝在一起已经
---------TSH ❤️ CXY---------
---------TSH
❤️
CXY---------
0 0 0 0 0 00
分类
标签
站点统计
文章
18
分类
8
标签
19
总字数
74,739
运行时长
0
最后活动
0 天前

文章目录

🤖 AI 助手

👋 你好!

我可以帮你解答关于这篇文章的问题