SpringBoot不讀取bootstrap.yml/properties文件問題
SpringBoot不讀取bootstrap.yml/properties文件
今天寫創(chuàng)建了一個SpringBoot項目,配置文件從其他項目拷貝了一份bootstrap.yml
之前一直用的application.yml
心想:application.yml 優(yōu)先級沒有bootstrap.yml 高,bootstrap配置文件肯定沒問題
項目一跑來,發(fā)現配置文件里面的內容沒讀取到。
之后通過各種查資料,才明白了application.yml 和bootstrap.yml 的區(qū)別,不僅僅是優(yōu)先級的問題。
先說我的問題是什么原因吧
SpringBoot 項目中如果沒有依賴 spring-cloud-context 的話,是不會讀取bootstrap.properties 文件
也就是說
bootstrap.yml配置是SpringCloud項目才會用到的。
如果你的項目僅僅是一個SpringBoot項目,只會識別application.yml配置文件。
由于SpringCloud是基于SpringBoot構建的,所有SpringCloud項目兩種文件都會識別,這個時候才有優(yōu)先級的說法,SpringCloud項目是會優(yōu)先讀取bootstrap配置在讀取application配置。
引用Spring官網的一句話
A Spring Cloud application operates by creating a “bootstrap” context, which is a parent context for the main application. Out of the box it is responsible for loading configuration properties from the external sources, and also decrypting properties in the local external configuration files.
SpringCloud 應用是基于 一個 “bootstrap”的上下文運行的。
也就說明了 bootstrap這個配置是SpringCloud 應用才會用的。
什么配置在application配置?什么配置在bootstrap里面配置?
1、當使用 Spring Cloud Config Server 的時候,spring.application.name 和 spring.cloud.config.server.git.uri應該在 bootstrap.yml 里面指定
2、一些加密解密的配置也應該配置在bootstrap里面
英文好的可以參考官網文章
SpringBoot中解析yml文件
springboot集成log4j2時,需要將操作日志記錄到數據源。這需要自己編寫數據源連接信息。如下:
不太優(yōu)雅,數據源是在配置文件中是有配置,可是這里又需要配置一次,而且后期切換環(huán)境給運維也增加挑戰(zhàn)。
怎么從配置文件獲取參數呢?甚至直接從容器中提取數據源?
@Value
? @Autowired
? getBean("dataSource")
?
nonono!!!
這很蛋疼了。springboot啟動時最開始就需要加載日志配置中的數據源信息,可是這時候spring所有的bean都還未加載,也無法通過任何方式在spring容器中獲取參數配置。
咋搞?小弟不才,通過硬寫解析yml配置文件的方法獲取配置文件中的數據源配置信息。
添加依賴
<dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-yaml</artifactId> <version>2.11.2</version> </dependency>
新增工具類
import org.springframework.util.ResourceUtils; import org.yaml.snakeyaml.Yaml; import java.io.File; import java.io.FileReader; import java.io.Reader; import java.util.Map; public class YmlUtil { public static Map<String,Object> getYaml(String filePath){ Yaml yml = new Yaml(); Reader reader = null; try { // String filePath = ResourceUtils.getURL("classpath:").getPath() + "/application.yml"; reader = new FileReader(new File(filePath)); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return yml.loadAs(reader, Map.class); } public static Object getPropValue(Map<String,Object> map,String key){ Map<String,Object> temp = map; int length = key.split("\\.").length; for (int i = 0; i < length; i++) { if(i==length-1){ //葉子節(jié)點直接獲取 return temp.get(key.split("\\.")[i]); }else{ temp = (Map<String, Object>)temp.get(key.split("\\.")[i]); } } return null; } //使用方法 public static void main(String[] args) { try{ Map<String,Object> map = getYaml(ResourceUtils.getURL("classpath:").getPath() + "/application.yml"); Object val = getPropValue(map,"spring.profiles.active"); }catch (Exception e){ } } }
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java中Elasticsearch 實現分頁方式(三種方式)
Elasticsearch是用Java語言開發(fā)的,并作為Apache許可條款下的開放源碼發(fā)布,是一種流行的企業(yè)級搜索引擎,這篇文章主要介紹了Elasticsearch實現分頁的3種方式,需要的朋友可以參考下2022-07-07基于java中stack與heap的區(qū)別,java中的垃圾回收機制的相關介紹
本篇文章小編將為大家介紹,基于java中stack與heap的區(qū)別,java中的垃圾回收機制的相關介紹,需要的可以參考一下2013-04-04Java AQS(AbstractQueuedSynchronizer)源碼解析
AbstractQueuedSynchronizer被稱為隊列同步器,簡稱為大家熟知的AQS,這個類可以稱作concurrent包的基礎。本文將通過剖析源碼來看看AQS是如何工作的,感興趣的可以了解一下2023-02-02