3springboot:springboot配置檔案(配置檔案佔位符、Profile、配置檔案的載入位置)
阿新 • • 發佈:2018-12-14
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檔案