spring boot的 yml和properties的对比

properties文件都需要写全,yml前面相同的可以不写,一层对应一层就好了。

对比下:

application.properties

application.yml

在yml文件中有些细节需要注意,冒号后面要空一格再写值,虽然在IDE中都会自动空一格。。

 

application.properties  文件和 application.yml 文件有什么区别呢?

yml文件的好处,天然的树状结构,一目了然,实质上跟properties是差不多的。

官方给的很多demo,都是用yml文件配置的。

注意点:

1,原有的key,例如spring.jpa.properties.hibernate.dialect,按“.”分割,都变成树状的配置

2,key后面的冒号,后面一定要跟一个空格

3,把原有的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

 

Spring Boot 虽然做了大量的工作来简化配置,但其配置依然是相当的复杂!
支持的外部配置方式就高达 17 种之多,当然这很灵活,但灵活就意味着复杂度的提升。
 
 这里只说说 application.yml 和 application.properties 两个文件的优先级
 
如果你的项目中存在 application.properties 文件,
那么 application.yml 文件就只是一个摆设。
 
为什么这么说呢?
我在 application.properties 文件中配置了:

server.port=8085

 

在 application.yml 文件中配置了:

server:

  port: 8086

启动项目,控制台输出:

main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8085 (http)

充分说明了这一点。

 

posted @ 2018-05-25 22:13  董永辉Bruno  阅读(26484)  评论(0编辑  收藏  举报