Table of Contents
配置查找#
Spring boot按以下顺序注入配置:- 命令行传递的配置
- 环境变量中定义的配置,格式为大写字母用
_分隔 jvm的-D配置- 打包后
jar包外部定义的配置文件 jar包同级目录下的配置文件src/main/resources/config包内的配置文件src/main/resources包内的配置文件
- 支持的配置文件:
application.yml/yaml/propertiesapplication-{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配置
spring配置#
datasource配置#
- 依赖于
spring-boot-starter-jdbc type:默认为Hikari连接池urlusernamepassworddriver-class-name:驱动类的全限定名hikari:连接池配置maximum-pool-size:最大连接数minimum-idle:最小空闲连接数connection-timeout:连接超时idle-timeout:空闲连接的超时max-lifetime:连接的最大生存时间
redis配置#
- 依赖于
spring-boot-starter-data-redis host:主机名timeout:连接超时connect-timeout:连接超时portpassworddatabase:数据库编号lettuce.pool:连接池配置max-active:最大激活连接数max-idle:最大空闲连接数min-idle
cache配置#
- 依赖于
spring-boot-starter-cache以及具体实现 type:采用的策略,可选caffeine、redis、simple、noneredis:redis的缓存配置time-to-live:缓存过期时间cache-null-values:是否缓存null
logging配置#
- 依赖于
spring-boot-starter-logging,默认使用logback level:root:默认日志级别org.springframework.web:指定包的日志级别,优先于rootcom.xxx:同上
file.name:日志文件路径pattern:日志编码(或者说输出格式)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 hostportusernamepasswordlistener:消息中间件的监听者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-originsallowed-methodsallowed-headersallow-credentials
security配置#
- 依赖于
spring-boot-starter-security oauth2:
其它配置#
application.name:应用名(用于服务发现)servlet.multipart:针对文件内容的配置max-file-size:最大文件大小max-request-size:请求文件的最大大小
jackson:json序列化配置
自定义配置导入#
- 此前通过
@PropertySource加载自定义配置文件 Spring Boot 2.4+更支持通过spring.config.import数组来加载- 通过
@ConfigurationProperties(prefix = "xxx")来将加载后的配置绑定到一个具体的配置类里