Spring配置文件
字
分钟
Spring配置文件
✨ AI 概括
点击按钮,AI 将为你生成这篇文章的摘要
定义
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: 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/Shanghaimybatis: 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.properties和application.yml可共存,优先级由SpringBoot加载顺序决定。 -
切换配置文件格式时,将原文件重命名(如改为
_application.properties)即可避免冲
支持与分享
如果这篇文章对你有帮助,欢迎分享给更多人或赞助支持!
相关文章 智能推荐
1
http
JavaWeb http学习之路开启
2
Ioc与DI
JavaWeb Ioc容器学习之路开启
3
Maven
JavaWeb Maven学习之路开启
4
Mybatis
JavaWeb Mybatis学习之路开启
5
Tlias管理系统
JavaWeb web系统学习之路开启
随机文章 随机推荐