SpringBoot框架:配置文件application.properties和application.yml的区别


一、格式

  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文件的配置信息,且后者会覆盖前者

  所以一般按照个人编写配置文件的格式习惯,选择一种进行使用就可以了。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM