学习SpringBoot(三)----YAML与SpringBoot

YAML与Spring Boot

1 YAML简介

YAML是一个可读性高,用来表达数据序列化的格式。Spring(以及其他主流 Java 框架)的配置文件从最开始的 XML 到后来的 Properties ,再到现在比较流行的 YAML 。一路的演变越来越关注数据本身。

2 YAML与Properties

示例来自 Spring Boot Reference Guide

Properties:

environments.dev.url=https://dev.example.com
environments.dev.name=Developer Setup
environments.prod.url=https://another.example.com
environments.prod.name=My Cool App

YAML:

environments:
        dev:
               url: https://dev.example.com
               name: Developer Setup
        prod:
               url: https://another.example.com
               name: My Cool App

3 YAML语法

3.1 基本规则

  • · 大小写敏感
  • · 使用空格缩进表示层级关系
  • · 缩进的空格数目没有要求,同级元素左侧对齐即可
  • · 使用 # 注释,只有行注释,没有块注释
  • · key 与 value 用 : 加上空格来分割

3.2 基本组件

  

YAML 基本组件主要就两种:

  • · 对象(映射/字典)
  • · 数组(列表)

对象:

person:
  name: John Smith
  age: 33

 

数组:

- apple
- banana

3.3 数据类型

  • · 字符串
  • · 布尔值
  • · 数值(整型、浮点)
  • · Null
  • · 日期时间(Date、Time)

示例:

number: 123
boolean: true
string: hello
null: ~
date: 2019-06-09

4 Spring Boot常用配置项

spring:
  # 数据源配置
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://127.0.0.1:3306/spring_boot
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
    name: druid
  druid:
    # 初始化大小,最小,最大
    initialSize: 5
    minIdle: 5
    maxActive: 20
    # 配置获取连接等待超时的时间
    maxWait: 60000
    # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
    timeBetweenEvictionRunsMillis: 60000
    # 配置一个连接在池中最小生存的时间,单位是毫秒
    minEvictableIdleTimeMillis: 300000
  
  # http请求
  http:
      #json序列化框架
    converters:
      preferred-json-mapper: fastjson
    #上传文件、请求最大限制
    multipart:
      max-file-size: 50MB
      max-request-size: 50MB

  # Spring MVC配置
  mvc:
    dispatch-options-request: true
    static-path-pattern: /**

  # 激活环境配置
  profiles:
    active: local

  # Redis配置
  redis:
    host: 127.0.0.1
    port: 6379

  # 应用名
  application:
    name: springboot

# Redis客户端
jedis:
  pool:
    host: 127.0.0.1
    port: 6379

# 线程池的配置文件
threads:
  pool:
    queueCapacity: 50
    corePoolSize: 20
    maxPoolSize: 50
    keepAliveSeconds: 600

# 日志级别
logging:
  level: info

# Web服务器
server:
  # 超时时间
  session:
    timeout: 60
  # 端口
  port: 8080
  # 根路径
  context-path: /springboot

 

posted @ 2020-05-18 20:47  mingmingn  阅读(256)  评论(0)    收藏  举报