3springboot:springboot配置文件(配置文件佔位符、Profile、配置文件的加載位置)

1.配置文件佔位符spring

RaandomValuePropertySourcr:配置文件能夠使用隨機數
      ${random.value}      ${random.int}  ${random.long}
      ${random.int(10)}    ${random.int[1024,65535]}
 
屬性配置佔位符
-能夠在配置文件中引用前面配置過的屬性(優先使用前面配置過的這裏都能使用)
- ${app.name:默認值} 來制定找不到屬性時的默認值

 

能夠用隨機數
能夠用佔位符獲取以前配置的值,沒有能夠指定默認值

 

 application.propertiesspringboot

#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.Profileapp

是spring對不一樣環境提供的不一樣配置功能的支持,能夠經過激活,
指定參數等方式快速切換環境
2.1多profile形式
---配置文件文件命能夠是 application-{profile}.properties/yml

默認使用application.propertiesdom

2.2多profile文檔切換模式spa

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指定激活的命令行3d

 

 

 3.配置文件的加載位置code

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文件