springboot多环境配置,Profile

Profile是Spring对不同环境提供不同配置功能的支持,可以通过激活、指定参数等方式快速切换环境1多profile文件形式:
-格式:application-{profile}.properties:
application-dev.properties、application-prod.properties

或者自定义application-xxxxx.properties

新建这四个配置文件首先我们测试 .properties的配置文件。

application.properties

server.port=9998

com.email=99@dfp.com
com.name=newDFP+++${com.email:不存在给默认值}
com.age=${random.int}


application-dev.properties

server.port=9999
spring.datasource.url=jdbc:mysql://localhost:3306/boot_crm?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=Hongkong
spring.datasource.data-username=root
spring.datasource.data-password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

application.yml

server:
  port: 8083

#引入其他yml文件
spring:
  profiles:
    active: datasource

  groovy:
    template:
      cache: false

com:
  example:
    mes: wyt!!!!aiaiai
  age: 18
  email: DFP@qq.com
  id: 991
  name: DFP
  list:
    - sss
    - qqq
    - www

application-datasource.yml

server:
  port: 8083
spring:
  dataSource:
    url: jdbc:mysql://localhost:3306/boot_crm?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=Hongkong
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver

直接启动控制类测试

端口是9998我们发现是在 application.properties配置的

所以当.properties和.yml同时存在的时候是.properties优先

我们在application.properties添加一句

spring.profiles.active=dev

他就会读取 application-dev.properties的配置信息并且沿用9998端口

因为优先级关系我们要注释掉.properties的配置文件测试yml的配置文件

直接测试因为我们在application.yml已经引入了其他yml配置文件。

#引入其他yml文件
spring:
  profiles:
    active: datasource

 直接部署在8089端口

有的时候我们也会单独创建一个配置文件放置数据源的配置信息这样让主配置文件看起来没有那么冗余。

yml的分模块配置

这次我们只在一个yml配置文件进行分模块

server:
  port: 8083
spring:
  profiles:
    active: dev
---
#引入其他yml文件
spring:
  profiles:
    active: datasource,test
  groovy:
    template:
      cache: false
---
com:
  example:
    mes: wyt!!!!aiaiai
  age: 18
  email: DFP@qq.com
  id: 991
  name: DFP
  list:
    - sss
    - qqq
    - www
---
#yml文件会以“---”划分模板块
server:
  port: 8077
spring:
  profiles: dev
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/boot_crm?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=Hongkong
    username: root
    password: 123456

当我们在第一个模块开启其他模块就会引用其他模块的信息

 我们修改配置信息

server:
  port: 8083
spring:
    datasource:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://localhost:3306/boot_crm?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=Hongkong
      username: root
      password: 123456

---
#引入其他yml文件
#spring:
#  profiles:
#    active: datasource,test
#  groovy:
#    template:
#      cache: false
---
com:
  example:
    mes: wyt!!!!aiaiai
  age: 18
  email: DFP@qq.com
  id: 991
  name: DFP
  list:
    - sss
    - qqq
    - www
---
#yml文件会以“---”划分模板块
server:
  port: 8077
spring:
  profiles: dev
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/boot_crm?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=Hongkong
    username: root
    password: 123456

端口改变

还有一种方式制定配置文件

命令行控制jar包

--spring.profiles.actvie=xx

这样就会自动到dev配置的端口

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值