关于springboot:springboot开发笔记7Profile多环境配置文件加载

4次阅读

共计 2992 个字符,预计需要花费 8 分钟才能阅读完成。

上一节: springboot 开发笔记 -(6)- 属性占位符以及默认值

7.1 properties 配置文件: 多个文件多环境

3 个配置文件: application.properties application-dev.properties application-prod.properties

第 1 个: application.properties

server.port=8888
spring.profiles.active=dev

person.firstName= 新名
person.lastName=${random.uuid}
person.age=${random.int(100)}
person.male=true
person.birth=1985/03/03
person.maps.k1=v1
person.maps.k2=20
person.maps.k3=true
person.lists=lisi,wangwu
person.dog.name=${person.firstName: 无名}_小 d
person.dog.age=${random.int[30,40]}

第 2 个: application-dev.properties

server.port=9999

person.lastName=${random.uuid}
person.age=${random.int(100)}
person.male=true
person.birth=1985/03/03
person.maps.k1=v1
person.maps.k2=20
person.maps.k3=true
person.lists=lisi,wangwu
person.dog.name=${person.firstName: 无名 dev}_小 dev
#person.dog.age=${random.int[10,20]}

第 3 个: application-prod.properties

server.port=7777

person.lastName=${random.uuid}
person.age=${random.int(100)}
person.male=true
person.birth=1985/03/03
person.maps.k1=v1
person.maps.k2=20
person.maps.k3=true
person.lists=lisi,wangwu
person.dog.name=${person.firstName: 无名 prod}_小 prod
person.dog.age=${random.int[20,30]}

利用启动的时候, 先去 第 1 个 配置文件 application.properties 中找 spring.profiles.active=dev 找到 dev 这样, 就会读取 application-dev.properties 的配置文件来加载; 那么问题来了:

7.2.1 配置文件加载程序是怎么?

  1. 先加载 application.properties 中的配置
  2. 而后加载 application-dev.properties 中的配置, 如果反复的, 用 dev 去笼罩下面的; 如果 dev 中没有的, application.properties 中的配置就失效!

7.2.2 dev 是 active 的话, application.properties 中其余配置还会加载吗?

  1. 答复如上: 会失效, 除非 dev 中的笼罩了;

下面三个配置文件设置后, person 对象的属性加载后果是: 可见 person.dog.age被 application.properties 的起作用了;

{"lastName":"94b68ef1-33d2-4866-9b62-32c5fb983f22","age":47,"male":true,"birth":"Mar 3, 1985 12:00:00 AM","maps":{"k1":"v1","k2":"20","k3":"true"},"lists":["lisi","wangwu"],"dog":{"name":"新名_小 dev","age":32}}

7.2 yml 配置文件: 一个文件多环境

spring:
  profiles:
    active: prod
person:
  firstName: newName
---
spring:
  profiles: dev
server:
  port: 5555

person:
  lastName: ${random.uuid} # uuid
  age: ${random.int(100)} # 0-100 的随机值
  male: true
  birth: 1985/03/03
  maps: {k1: v1, k2: 20, k3: true}
  lists:
    - lisi
    - wangwu
  dog:
    name: ${person.firstName: 无名}_小黄 dev #如果为空, 取默认值
    age: ${random.int[1,3]} #10-20 之间的随机值: 两头还不能有空格, 实测!
---
spring:
  profiles: prod
server:
  port: 6666

person:
  lastName: ${random.uuid} # uuid
  age: ${random.int(100)} # 0-100 的随机值
  male: true
  birth: 1985/03/03
  maps: {k1: v1, k2: 20, k3: true}
  lists:
    - lisi
    - wangwu
  dog:
    name: ${person.firstName: 无名}_小黄 prod #如果为空, 取默认值
    age: ${random.int[4,6]} #10-20 之间的随机值: 两头还不能有空格, 实测!

---能够作为文档宰割符号

  1. 这个配置中, 配置文件 yml 被分成 3 个 document: 用 spring.profiles 辨别:
  2. active: prod 指定激活 prod 的配置;
  3. 笼罩逻辑跟下面 properties 一样

问题来了:

7.2.1 yml 和 application 哪个先加载? 什么关系?

  1. 两个都会读取;
  2. application.yml 先于 application.properties 加载; (示例证实如下)
  3. 因为加载有先后关系, properties 的属性值会笼罩 yml 中的;

在下面 application.yml的根底上, 如果 application.properties 中也有配置:

person.firstName= 新名

则最终后果是:

{"lastName":"6d3a8f32-7736-48c8-9fb9-9fb3f73da6b7","age":36,"male":true,"birth":"Mar 3, 1985 12:00:00 AM","maps":{"k1":"v1","k2":20,"k3":true},"lists":["lisi","wangwu"],"dog":{"name":"新名_小黄 prod","age":4}}

阐明了下面的论断: application.properties 中的

person.firstName= 新名 笼罩了 application.yml 中的 person.firstName: newName; 同时也证实两个都会加载!

7.3 还能够通过启动加参数激活 active

会笼罩我的项目中的配置:

java -jar xxx.jar -Dspring.profiles.active=prod

正文完
 0