天天看点

Spring Boot(二):Spring Boot中的配置参数

Spring Boot 配置参数

Spring Boot 帮助我们完成了许许多多的自动化配置 如果我们需要根据自己的需求修改配置 也是可以的

可以使用.properties 和 .yml 格式配置 这里只列出几个常用的配置项 具体大家可以去官网查询

配置文件

Key值 默认值 描述
server.address 服务器的网络地址
server.port 8080 端口
server.error.path /error 当系统出错时需要跳转的地址
server.error.whitelabel.enabled true 是否使用默认的错误页面
server.max-http-header-size 8KB HTTP请求头容量限制
server.servlet.application-display-name application 应用名称
server.servlet.session.cookie.name Session名称
server.servlet.session.cookie.path Session路径
server.servlet.session.timeout 30m Session超时时间
spring.config.additional-location 需要额外添加配置文件的地址
spring.config.location 默认配置文件的地址
spring.config.name 配置文件名称

PS:这里并没有列出 例如数据库配置 日志 等相关的配置 会在以后的文章中讲到相关内容时添加

在我们实际项目的开发中 我们一般分为本地 开发 测试 仿真 生产等环境(根据不同公司 会有不同的阶段) 这些环境往往都会有一下差异例如数据库的链接地址 文件的上传地址 等等 我们不可能为了根据发布的环境 经常去该配置文件 所以Spring也给我们提供了 根据不同环境 启用不同的配置文件的功能
首先在resources下面创建几个自己需要的配置文件 有几个配置文件 我们配置几个 文件以application-环境命名
Spring Boot(二):Spring Boot中的配置参数

然后再 application.properties 中添加 spring.profeiles.active = prod

启动查看日志

Spring Boot(二):Spring Boot中的配置参数
java -jar xxxxx.jar --spring.profiles.active=dev 
           
java -jar xxxxx.jar --spring.profiles.active=dev,local 
           
JAVA_OPTS="-Dspring.profiles.active=prod"