本篇文章給大家分享的是有關(guān)使用springboot如何實(shí)現(xiàn)獲取配置文件中的屬性值,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
10年積累的成都做網(wǎng)站、成都網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先網(wǎng)站策劃后付款的網(wǎng)站建設(shè)流程,更有云溪免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
在spring boot中,簡單幾步,讀取配置文件(application.yml)中各種不同類型的屬性值:
1、引入依賴:
<!-- 支持 @ConfigurationProperties 注解 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
2、配置文件(application.yml)中配置各個屬性的值:
myProps: #自定義的屬性和值 simpleProp: simplePropValue arrayProps: 1,2,3,4,5 listProp1: - name: abc value: abcValue - name: efg value: efgValue listProp2: - config2Value1 - config2Vavlue2 mapProps: key1: value1 key2: value2
3、創(chuàng)建一個bean來接收配置信息:
@Component @ConfigurationProperties(prefix="myProps") //接收application.yml中的myProps下面的屬性 public class MyProps { private String simpleProp; private String[] arrayProps; private List<Map<String, String>> listProp1 = new ArrayList<>(); //接收prop1里面的屬性值 private List<String> listProp2 = new ArrayList<>(); //接收prop2里面的屬性值 private Map<String, String> mapProps = new HashMap<>(); //接收prop1里面的屬性值 public String getSimpleProp() { return simpleProp; } //String類型的一定需要setter來接收屬性值;maps, collections, 和 arrays 不需要 public void setSimpleProp(String simpleProp) { this.simpleProp = simpleProp; } public List<Map<String, String>> getListProp1() { return listProp1; } public List<String> getListProp2() { return listProp2; } public String[] getArrayProps() { return arrayProps; } public void setArrayProps(String[] arrayProps) { this.arrayProps = arrayProps; } public Map<String, String> getMapProps() { return mapProps; } public void setMapProps(Map<String, String> mapProps) { this.mapProps = mapProps; } }
啟動后,這個bean里面的屬性就會自動接收配置的值了。
4、單元測試用例:
@Autowired private MyProps myProps; @Test public void propsTest() throws JsonProcessingException { System.out.println("simpleProp: " + myProps.getSimpleProp()); System.out.println("arrayProps: " + objectMapper.writeValueAsString(myProps.getArrayProps())); System.out.println("listProp1: " + objectMapper.writeValueAsString(myProps.getListProp1())); System.out.println("listProp2: " + objectMapper.writeValueAsString(myProps.getListProp2())); System.out.println("mapProps: " + objectMapper.writeValueAsString(myProps.getMapProps())); }
測試結(jié)果:
simpleProp: simplePropValue arrayProps: ["1","2","3","4","5"] listProp1: [{"name":"abc","value":"abcValue"},{"name":"efg","value":"efgValue"}] listProp2: ["config2Value1","config2Vavlue2"] mapProps: {"key1":"value1","key2":"value2"}
以上就是使用springboot如何實(shí)現(xiàn)獲取配置文件中的屬性值,小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
名稱欄目:使用springboot如何實(shí)現(xiàn)獲取配置文件中的屬性值
分享網(wǎng)址:http://muchs.cn/article46/jehehg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、、全網(wǎng)營銷推廣、網(wǎng)站制作、網(wǎng)站設(shè)計、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)