天天看點

SpringBoot之淺析配置項解析(三)

我們接着上一篇的文章繼續分析。我們來看這一段代碼:

我們先來看1)的代碼:this.propertiesLoader.getAllFileExtensions()這一句。

在代碼1)處 this.loaders是什麼呢?在ConfigFileApplicationListener.Loader#load()這個方法中有這樣一段代碼:

是以1處的this.loaders 是 元素為 PropertiesPropertySourceLoader 和YamlPropertySourceLoader的List。2)處的loader.getFileExtensions()的代碼分别如下:

是以getAllFileExtensions() 得到的擴充名為含有 “properties”, “xml” ,”yml”, “yaml” 這個四個元素的一個集合。是以我們接着上一章簡化的代碼,在這裡進一步的簡化代碼為:

看到上面的代碼,讀者應該明白SpringBoot加載的配置檔案的位置和檔案類型了吧?我們繼續到loadIntoGroup看一下它的代碼:

org.springframework.boot.env.PropertySourcesLoader#load

如果我們的配置檔案是這樣的:

SpringBoot之淺析配置項解析(三)

那麼我們最終得到的EnumerableCompositePropertySource 是什麼樣的呢?

SpringBoot之淺析配置項解析(三)

注意了!!!注意了!!!是properties在前,yml在後說明properties的優先級比yml高!!!!!

我們再來看一下這一段的代碼,在org.springframework.boot.context.config.ConfigFileApplicationListener.Loader#load()中

講過上面的分析,我們可得到一個不同的配置項在環境變量中所處的一個位置情況:commandLineArgs、servletConfigInitParams 、servletContextInitParams 、jndiProperties 、systemProperties 、systemEnvironment、random、applicationConfigurationProperties、defaultProperties。 applicationConfigurationProperties中元素的位置順序 file:./config/、file:./、classpath:/config/、classpath:/,其中properties字尾的檔案在前,yml在後。