一、格式
1、application.properties格式:
server.port=8080 server.servlet.context-path=/cn spring.datasource.url=jdbc:mysql://localhost:3306/spring_boot_demo?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver
2、application.yml格式:
server: port: 8080 servlet: context-path: /cn spring: datasource: url: jdbc:mysql://localhost:3306/spring_boot_demo?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai username: root password: root driver-class-name: com.mysql.jdbc.Driver
3、區別:
.properties文件使用“ . ”來遞進,使用“ = ”來進行配置賦值,沒有明顯的層次感。
.yml文件使用換行和縮進來遞進,使用“ : ”來進行賦值(冒號后要空一格),格式要求比較嚴格,有明顯的層次感。
二、執行順序
在運行程序時,會先讀取.yml文件的配置信息,再讀取.properties文件的配置信息,且后者會覆蓋前者。
所以一般按照個人編寫配置文件的格式習慣,選擇一種進行使用就可以了。