配置查找

  • Spring boot按以下顺序注入配置:
    • 命令行传递的配置
    • 环境变量中定义的配置,格式为大写字母用_分隔
    • jvm的-D配置
    • 打包后jar包外部定义的配置文件
    • jar包同级目录下的配置文件
    • src/main/resources/config包内的配置文件
    • src/main/resources包内的配置文件
  • 支持的配置文件:
    • application.yml/yaml/properties
    • application-{profile}.yml/yaml/properties:特定命名空间的配置文件,profile可完全自定义,通常通过注入环境变量来区分环境或分离配置
    • application-test.yml/yaml/properties:仅测试环境会加载
  • messages国际化消息文件:
    • i18n/messages.properties:默认的消息文件,可以通过修改spring.messages.basename来修改路径
    • i18n/messages_zh_CN.properties:特定语言的消息文件,会优先搜索

server配置

  • 依赖于spring-boot-starter-web
  • port:端口
  • servlet:服务的配置
    • context-path:服务的上下文路径,如/api/v1
  • tomcat:内嵌的tomcat配置,默认开启(即本身是一个jar包而不是war包)
    • accept-count:等待队列的长度
    • threads.min-space:最小的空闲线程数
    • max-connections:最大连接数
    • max-threads:最大线程数
    • connection-timeout:连接超时
  • compression.enabled:响应压缩启用/禁用
  • ssl:https配置
    • enabled:启用https

spring配置

datasource配置

  • 依赖于spring-boot-starter-jdbc
  • type:默认为Hikari连接池
  • url
  • username
  • password
  • driver-class-name:驱动类的全限定名
  • hikari:连接池配置
    • maximum-pool-size:最大连接数
    • minimum-idle:最小空闲连接数
    • connection-timeout:连接超时
    • idle-timeout:空闲连接的超时
    • max-lifetime:连接的最大生存时间

redis配置

  • 依赖于spring-boot-starter-data-redis
  • host:主机名
  • timeout:连接超时
  • connect-timeout:连接超时
  • port
  • password
  • database:数据库编号
  • lettuce.pool:连接池配置
    • max-active:最大激活连接数
    • max-idle:最大空闲连接数
    • min-idle

cache配置

  • 依赖于spring-boot-starter-cache以及具体实现
  • type:采用的策略,可选caffeine、redis、simple、none
  • redis:redis的缓存配置
    • time-to-live:缓存过期时间
    • cache-null-values:是否缓存null

logging配置

  • 依赖于spring-boot-starter-logging,默认使用logback
  • level:
    • root:默认日志级别
    • org.springframework.web:指定包的日志级别,优先于root
    • com.xxx:同上
  • file.name:日志文件路径
  • pattern:日志编码(或者说输出格式)
    • console
    • file
  • logback.rollingpolicy:
    • max-file-size:单个日志文件最大大小
    • max-history:日志保留的最长时间

jpa配置

  • 依赖于spring-boot-starter-data-jpa
  • hibernate:经典实现hibernate的配置
    • ddl-auto:update每次启动时更新表,create每次启动时创建表,none表示什么也不干
  • show-sql:表示是否将自动生成的sql显示出来

rabbitmq配置

  • 依赖于spring-boot-starter-amqp
  • host
  • port
  • username
  • password
  • listener:消息中间件的监听者
    • simple:
      • concurrency:最小消费者数量
      • max-concurrency:最大消费者数量

rocketmq配置

  • 依赖于rocketmq-spring-boot-starter

eureka服务发现

  • 依赖于spring-cloud-starter-netflix-eureka-client

分布式配置中心

  • 依赖于spring-cloud-starter-config

mybatis配置

  • 依赖于mybatis-spring-boot-starter

web.cors配置

  • allowed-origins
  • allowed-methods
  • allowed-headers
  • allow-credentials

security配置

  • 依赖于spring-boot-starter-security
  • oauth2:
    • resourceserver.jwt:

其它配置

  • application.name:应用名(用于服务发现)
  • servlet.multipart:针对文件内容的配置
    • max-file-size:最大文件大小
    • max-request-size:请求文件的最大大小
  • jackson:json序列化配置

自定义配置导入

  • 此前通过@PropertySource加载自定义配置文件
  • Spring Boot 2.4+更支持通过spring.config.import数组来加载
  • 通过@ConfigurationProperties(prefix = "xxx")来将加载后的配置绑定到一个具体的配置类里