4、springboot:springboot配置文件(配置文件占位符、Profile、配置文件的加载位置)

1.配置文件占位符

RaandomValuePropertySourcr:配置文件可以使用随机数
      ${random.value}      ${random.int}  ${random.long}
      ${random.int(10)}    ${random.int[1024,65535]}
 
属性配置占位符
-可以在配置文件中引用前面配置过的属性(优先使用前面配置过的这里都能使用)
-${app.name:默认值} 来制定找不到属性时的默认值
可以用随机数
可以用占位符获取之前配置的值,没有可以指定默认值

 application.properties

复制代码
#person
person.name=无敌多么寂寞
person.age=${random.int}
person.bir=2018/12/11
person.boss=true
person.map.q1=1
person.map.q2=2
person.lists=a,b,c
person.dog.name=${person.name}*cat
person.dog.age=22
复制代码
@Autowired
Person person;
@Test
public void contextLoads() {
    System.out.println(person);
}

此时没有person.mr可以进行默认设置
复制代码
#person
person.name=无敌多么寂寞
person.age=${random.int}
person.bir=2018/12/11
person.boss=true
person.map.q1=1
person.map.q2=2
person.lists=a,b,c
person.dog.name=${person.mr:mr}*cat
person.dog.age=22
复制代码

 

2.Profile

  是spring对不同环境提供的不同配置功能的支持,可以通过激活,
  指定参数等方式快速切换环境
  2.1多profile形式
  ---配置文件文件命可以是 application-{profile}.properties/yml

默认使用application.properties

  2.2多profile文档切换模式

复制代码
server:
  port: 8088
spring:
  profiles:
    active: dev
---
server:
  port: 8090
spring:
  profiles:dev  //指定属于那个文档库
复制代码

 

   2.3激活方式

 

    2.3.1 配置文件中使用spring.profiles.active=dev

 

    2.3.2yml文档块(使用---分隔文档块)
复制代码
server:
  port: 8088
spring:
  profiles:
    active: dev
---
server:
  port: 8090
spring:
  profiles:dev  //指定属于那个文档库
复制代码

 

     2.3.3指定激活的命令行

 

 3.配置文件的加载位置

springboot 启动会扫描以下位置的application.properties或者application.yml文件作为Spring boot的默认配置文件
file: ./config/               项目目录下的config
file: ./                  项目目录下
classpath:/config/             resources目录下的config
classpath:/                resources目录下
优先级由高到底高优先级的配置会覆盖低优先级的配置
SpringBoot会从这四个位置全部加载主配置文件;互补配置
我们还可以通过spring.config.location来改变默认的配置文件位置

 

 

通过spring.config.location来改变默认的配置文件位置:
引入外部的xxxx.properties文件

 

摘自:https://www.cnblogs.com/Mrchengs/p/10120140.html

原文地址:https://www.cnblogs.com/lyh233/p/12489352.html