天天看点

SpringBoot配置文件占位符/Profile

配置文件占位符

  1. 随机数
${random.value}/${random.int}、${ramdom.long}
           
  1. 占位符获取之前配置的值,如果没有可以使用冒号指定默认值
${person.hello:abc}
           

person没有hello的属性,冒号指定默认值,最后输出abc。

Profile

Profile是Spring对不同环境提供不同配置功能的支持,可以通过激活、指定参数等方式快速切换环境。

  1. 多Profile文件

    在主配置文件编写的时候,文件名可以是 application-{profile}.properties/yml

    就可以动态进行切换

    默认使用application.properties;

  2. yml支持多文档块模式
server:
  port: 8081
spring:
  profiles:
    active: prod
  
---
server:
  port: 8082
spring:
  profiles: dev
  
---
server:
  port: 8083
spring:
  profiles: prod
           
  1. 激活指定profile
    1. 在配置文件中指定:在配置文件中使用spring.profiles.active=xxx可以激活指定profile
    2. 命令行:

      -spring.profiles.active=dev(运行jar包的时候,可以直接在测试的时候,配置传入命令行参数)优先级高于1;

    3. 在编译器Program arguments中指定:—apring.profiles.active=xxx环境;并且这个优先级高于1;
    4. 虚拟机参数:编译器VM options:-Dspring.profile.active=xxx环境;

配置文件加载位置:

springboot启动会扫描以下位置的application.properties或者application.yml文件作为SpringBoot的默认配置。