加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

基于Spring Boot不同的环境使用不同的配置方法

发布时间:2020-12-14 20:07:12 所属栏目:Java 来源:网络整理
导读:spring 多文件配置: 1、properties文件 2、YAML文件 一、properties文件 在 Spring Boot 中, 多环境配置的文件名需要满足 application-{profile}. properties的格式, 其中{profile}对应你的环境标识, 如下所示。 application-dev.properties: 开发环境。

spring 多文件配置:

1、properties文件

2、YAML文件

一、properties文件

在 Spring Boot 中, 多环境配置的文件名需要满足 application-{profile}.

properties的格式, 其中{profile}对应你的环境标识, 如下所示。

• application-dev.properties: 开发环境。

• application-test.properties: 测试环境。

• application-prod.properties: 生产环境。

至于具体哪个配置文件会被加载, 需要在 app巨ca巨on.properties 文件中通过

spring.profiles.active 属性来设置, 其 值 对应配置文件中的{profile}值。 如

spring.profiles.active = test就会加载 application-test.properties配置

文件内容。

二、YAML文件

server: 
 port: 8080 
# 默认的profile为dev,其他环境通过指定启动参数使用不同的profile,比如: 
# 测试环境:java -jar xxx.jar --spring.profiles.active=test 
# 生产环境:java -jar xxx.jar --spring.profiles.active=prod 
spring: 
 profiles: 
 active: dev 
#下面这一行务必不能少,区分不同配置,而且必须是三个字符"-"
---
# 开发环境配置
spring:
 profiles: dev
 datasource:
 url: jdbc:mysql://192.168.0.152:3306/aylson?useUnicode=true&characterEncoding=UTF-8&useSSL=false

---
# 测试环境配置
spring:
 profiles: test
 datasource:
 url: jdbc:mysql://192.168.0.152:13306/aylson?useUnicode=true&characterEncoding=UTF-8&useSSL=false

---
# 生产环境配置
spring:
 profiles: prod
 datasource:
 url: jdbc:mysql://192.168.0.152:23306/aylson?useUnicode=true&characterEncoding=UTF-8&useSSL=false

使用方法:

通过指定启动参数使用不同的profile,比如:

测试环境:Java -jar xxx.jar Cspring.profiles.active=test

生产环境:java -jar xxx.jar Cspring.profiles.active=prod

以上这篇基于Spring Boot不同的环境使用不同的配置方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程小技巧。

您可能感兴趣的文章:

  • Springboot整合Dubbo教程之项目创建和环境搭建
  • spring boot 下支付宝的开箱既用环境
  • 详解Spring Boot配置文件之多环境配置
  • 详解用maven搭建springboot环境的方法
  • 简述springboot及springboot cloud环境搭建
  • 详解springboot + profile(不同环境读取不同配置)
  • macOS下Spring Boot开发环境搭建教程

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读