0
点赞
收藏
分享

微信扫一扫

springboot多环境配置,Profile

香小蕉 2022-01-08 阅读 135

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端口

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

举报

相关推荐

0 条评论