原文地址:https://www.cnblogs.com/dyh-air/articles/9090882.html
目錄
properties文件都需要寫全,yml前面相同的可以不寫,一層對應一層就好了。
對比如下:
application.properties:
server.port=8080
server.context-path=/test
spring.datasource.url=jdbc:mysql://127.0.0.1/3306
application.yml:
server:
port: 8080
context-path: /test
spring:
datasource:
url: jdbc:mysql://127.0.0.1/3306
在yml文件中有些細節需要注意,冒號后面要空一格再寫值,雖然在IDE中都會自動空一格。
application.properties文件和application.yml文件有什么區別呢?
yml文件的好處:天然的樹狀結構,一目了然,實質上和properties是差不多的。
官方給的很多demo,都是用yml文件配置的:
- 原有的key,例如spring.jpa.properties.hibernate.dialect,按“.”分割,都變成樹狀的配置
- key后面的冒號,后面一定要跟一格空格
- 把原有的application.properties刪掉,然后一定要執行一下 maven -X clean install
# application.yml
server:
port: 8086
spring:
datasource:
name: test
url: jdbc:mysql://192.168.1.112:3306/test
username: root
password: xxx
# 使用druid數據源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
# application.properties
server.port=8085
spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
spring.datasource.url=jdbc:mysql://aliyuncs.com:3306/home?useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true
spring.datasource.username=root
spring.datasource.password=***
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# mybatis.mapper-locations=classpath*:com/wanyu/fams/mapping/*Mapper.xml
# mybatis.type-aliases-package=com.wanyu.fams.model
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
spring.druid.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.druid.datasource.driverClassName=com.mysql.jdbc.Driver
spring.druid.datasource.url=jdbc:mysql://localhost:3306/spring_boot?characterEncoding=utf-8
spring.druid.datasource.username=root
spring.druid.datasource.password=xxx
springboot雖然做了大量的工作來簡化配置,但其配置依然是相當的復雜。
支持的外部配置方式就高達17種之多,當然這很靈活,但靈活就意味着復雜度的提升。
這里只說說application.yml和application.properties兩個文件的優先級。
如果你的項目中存在application.properties文件,那么application.yml文件就只是一個擺設。