SpringBoot解析指定Yaml配置文件的實現(xiàn)過程
下面還有投票,幫忙投個票??
前言
最近在看某個開源項目代碼并準(zhǔn)備參與其中,代碼過了一遍后發(fā)現(xiàn)多個自定義的配置文件用來裝載業(yè)務(wù)配置代替數(shù)據(jù)庫查詢,直接響應(yīng)給前端,這里簡單記錄一下實現(xiàn)過程。
我們通常在SpringBoot項目中用配置文件屬性時使用@ConfigurationProperties或@Value默認(rèn)配置文件的屬性值,也就是application.yml或者application.properties文件中的屬性值。
但是不能全都往默認(rèn)配置文件里堆的,本文利用@PropertySource和@ConfigurationProperties注解引用其它配置文件的屬性值。
1、自定義配置文件
在resources下創(chuàng)建my.yaml文件,“-”用來表示數(shù)組類型,一定要注意空格。
my: contents: - id: 12121 name: nadasd - id: 3333 name: vfffff
2、配置對象類
創(chuàng)建配置類對象,在類上添加@Component、@PropertySource、@ConfigurationProperties注解。
@Component是將該類交由spring管理,@PropertySource用來指定配置文件及解析Yaml格式,@ConfigurationProperties是將解析后的配置文件屬性自動注入該類的屬性。
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.List; @Component @PropertySource(value = "classpath:my.yaml", factory = YamlPropertiesSourceFactory.class) @ConfigurationProperties(prefix = "my") public class MyProperties { private List<content> contents = new ArrayList<>(); public List<content> getContents() { return contents; } public void setContents(List<content> contents) { this.contents = contents; } } class content { private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
@PropertySource注解是Spring用于加載配置文件,@PropertySource屬性如下:
- name:默認(rèn)為空,不指定Spring自動生成
- value:配置文件
- ignoreResourceNotFound:沒有找到配置文件是否忽略,默認(rèn)false,4.0版本加入
- encoding:配置文件編碼格式,默認(rèn)UTF-8 4.3版本才加入
- factory:配置文件解析工廠,默認(rèn):PropertySourceFactory.class 4.3版本才加入,如果是之前的版本就需要手動注入配置文件解析Bean
Spring Boot 默認(rèn)不支持@PropertySource讀取yaml 文件,需要自定義PropertySourceFactory進(jìn)行解析。
3、YamlPropertiesSourceFactory
創(chuàng)建YamlPropertiesSourceFactory類用來解析Yaml格式的文件。
import org.springframework.boot.env.YamlPropertySourceLoader; import org.springframework.core.env.PropertySource; import org.springframework.core.io.support.EncodedResource; import org.springframework.core.io.support.PropertySourceFactory; import java.io.IOException; import java.util.List; import java.util.Optional; public class YamlPropertiesSourceFactory implements PropertySourceFactory { @Override public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException { String resourceName = Optional.ofNullable(name).orElse(resource.getResource().getFilename()); List<PropertySource<?>> yamlSources = new YamlPropertySourceLoader().load(resourceName, resource.getResource()); return yamlSources.get(0); } }
到此這篇關(guān)于SpringBoot解析指定Yaml配置文件的實現(xiàn)過程的文章就介紹到這了,更多相關(guān)SpringBoot解析Yaml配置文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java生成隨機(jī)數(shù)之Random與ThreadLocalRandom性能比較詳解
大家項目中如果有生成隨機(jī)數(shù)的需求,我想大多都會選擇使用Random來實現(xiàn),它內(nèi)部使用了CAS來實現(xiàn)。?實際上,JDK1.7之后,提供了另外一個生成隨機(jī)數(shù)的類ThreadLocalRandom,那么他們二者之間的性能是怎么樣的呢?本文就來詳細(xì)說說2022-12-12一小時迅速入門Mybatis之Prepared Statement與符號的使用
這篇文章主要介紹了一小時迅速入門Mybatis之Prepared Statement與符號的使用,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09IDEA中如何查找jar包之間的依賴關(guān)系并忽略依賴的某個包
這篇文章主要介紹了IDEA中如何查找jar包之間的依賴關(guān)系并忽略依賴的某個包?本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08List集合中對數(shù)據(jù)實現(xiàn)多重規(guī)則進(jìn)行排序的案例
今天小編就為大家分享一篇關(guān)于List集合中對數(shù)據(jù)實現(xiàn)多重規(guī)則進(jìn)行排序的案例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12