spring boot配置文件:.yml与.properties
程序员文章站
2022-04-30 11:12:16
...
SpringBoot使用一个以application命名的配置文件作为默认的全局配置文件。支持properties后缀结尾的配置文件或者以yml/yaml后缀结尾的YAML的文件配置。
application.properties:
server.port=8002
server.tomcat.uri-encoding=UTF-8
server.tomcat.max-threads=1000
server.tomcat.min-spare-threads=30
spring.application.name=test
spring.datasource.name=druidDataSource
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=utf-8&useSSL=false
spring.datasource.druid.username=root
spring.datasource.druid.password=root
spring.datasource.druid.filters=stat
spring.datasource.druid.max-active=100
spring.datasource.druid.initial-size=1
spring.datasource.druid.max-wait=60000
spring.datasource.druid.min-idle=1
spring.datasource.druid.time-between-eviction-runs-millis=60000
spring.datasource.druid.min-evictable-idle-time-millis=300000
spring.datasource.druid.validation-query=select 1
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.test-on-borrow=false
spring.datasource.druid.test-on-return=false
spring.datasource.druid.pool-prepared-statements=true
spring.datasource.druid.max-open-prepared-statements=50
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20
application.yml:
# tomcat
server:
port: 8002
tomcat:
uri-encoding: UTF-8
max-threads: 1000
min-spare-threads: 30
# spring boot admin
spring:
application:
name: test
datasource:
name: druidDataSource
type: com.alibaba.druid.pool.DruidDataSource
druid:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
filters: stat
max-active: 100
initial-size: 1
max-wait: 60000
min-idle: 1
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
validation-query: select 1
test-while-idle: true
test-on-borrow: false
test-on-return: false
pool-prepared-statements: true
max-open-prepared-statements: 50
max-pool-prepared-statement-per-connection-size: 20
对比之后相信都看得出来,其实application.properties的功能和application.yml是一样的,不过因为yml文件是树状结构,写起来有更好的层次感,更易于理解,可读性更好一些,所以很多人都选择了yml文件。
值得注意的是同一目录下,properties配置优先级 > YAML配置优先级。
分享一个在线properties 转 yml工具,也支持yml转properties,地址:http://www.toyaml.com/index.html。
推荐阅读
-
spring boot application properties配置实例代码详解
-
详解spring boot starter redis配置文件
-
Spring Boot项目实战之拦截器与过滤器
-
spring boot启动时加载外部配置文件的方法
-
干货分享:ASP.NET CORE(C#)与Spring Boot MVC(JAVA)异曲同工的编程方式总结
-
Spring Boot 2 - 初识与新工程的创建
-
spring Boot环境下dubbo+zookeeper的一个基础讲解与示例
-
spring boot静态变量注入配置文件详解
-
Spring Boot 配置文件详解(小结)
-
Spring Boot与Spark、Cassandra系统集成开发示例