SPRINGBOOT讀取PROPERTIES配置文件數(shù)據(jù)過(guò)程詳解
這篇文章主要介紹了SPRINGBOOT讀取PROPERTIES配置文件數(shù)據(jù)過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
一.使用@ConfigurationProperties來(lái)讀取
1、Coffer entity
@Configuration @ConfigurationProperties(prefix = "coffer") @PropertySource("classpath:config/coffer.properties") public class Coffer { private String brand; private Double length; private Double width; private Double height; //省略了get/set方法 private String[] contains; private ArrayList<Fruit> fruits; private HashMap<String,Object> map; }
2、Fruit entity
@Configuration @ConfigurationProperties(prefix = "coffer.fruits") @PropertySource("classpath:config/coffer.properties") public class Fruit { private String fruitName; private String fruitColor; //省略了get/set方法 }
3、coffer.properties
coffer.brand=Camel coffer.length=100.00 coffer.width=80.00 coffer.height=60.00 coffer.contains[0]=Raincoat coffer.contains[1]=trousers coffer.contains[2]=hat coffer.contains[3]=glove coffer.contains[4]=scarf coffer.contains[5]=hood coffer.fruits[0].fruitName=apricot coffer.fruits[0].fruitColor=yellow coffer.fruits[1].fruitName=plum coffer.fruits[1].fruitColor=green coffer.fruits[2].fruitName=pineapple coffer.fruits[2].fruitColor=yellow coffer.fruits[3].fruitName=watermelon coffer.fruits[3].fruitColor=green coffer.fruits[4].fruitName=strawberry coffer.fruits[4].fruitColor=red coffer.map.name=xiaomao coffer.map.age=22 coffer.map.gender=female
4、springbootApplicationTest
@SpringBootTest class SpringbootApplicationTests { @Autowired private ApplicationContext ioc; @Autowired private Coffer coffer; @Test public void springbootTest(){ System.out.println(coffer); } }
5、result
Coffer{ brand='Camel', length=100.0, width=80.0, height=60.0, contains=[Raincoat, trousers, hat, glove, scarf, hood], fruits=[ Fruit{fruitName='apricot', fruitColor='yellow'}, Fruit{fruitName='plum', fruitColor='green'}, Fruit{fruitName='pineapple', fruitColor='yellow'}, Fruit{fruitName='watermelon', fruitColor='green'}, Fruit{fruitName='strawberry', fruitColor='red'} ], map={age=22, gender=female, name=xiaomao}}
二、使用@Value來(lái)讀取
在springTest中無(wú)法使用@Value來(lái)讀取配置屬性,需要放到Controller中去讀取
@PropertySource("classpath:config/coffer.properties") @RestController public class SpringbootController { @Value("${coffer.brand}") private String brand; @Value("${coffer.height}") private Double height; @RequestMapping("/test") public String springbootTest() { return brand+"====="+height; } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:Java多線(xiàn)程并發(fā)編程和鎖原理解析
欄 目:Java
下一篇:springmvc級(jí)聯(lián)屬性處理無(wú)法轉(zhuǎn)換異常問(wèn)題解決
本文標(biāo)題:SPRINGBOOT讀取PROPERTIES配置文件數(shù)據(jù)過(guò)程詳解
本文地址:http://mengdiqiu.com.cn/a1/Java/8911.html
您可能感興趣的文章
- 01-10Springboot中@Value的使用詳解
- 01-10springboot實(shí)現(xiàn)文件上傳步驟解析
- 01-10springboot jta atomikos實(shí)現(xiàn)分布式事物管理
- 01-10SpringBoot使用RabbitMQ延時(shí)隊(duì)列(小白必備)
- 01-10如何基于SpringBoot部署外部Tomcat過(guò)程解析
- 01-10springboot集成fastDfs過(guò)程代碼實(shí)例
- 01-10springboot 配置DRUID數(shù)據(jù)源的方法實(shí)例分析
- 01-10springboot2.0使用Hikari連接池的方法(替換druid)
- 01-10springboot單元測(cè)試兩種方法實(shí)例詳解
- 01-10Springboot測(cè)試類(lèi)沒(méi)有bean注入問(wèn)題解析


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10Java實(shí)現(xiàn)動(dòng)態(tài)模擬時(shí)鐘
- 01-10Springboot中@Value的使用詳解
- 01-10JavaWeb實(shí)現(xiàn)郵件發(fā)送功能
- 01-10利用Java實(shí)現(xiàn)復(fù)制Excel工作表功能
- 01-10Java實(shí)現(xiàn)動(dòng)態(tài)數(shù)字時(shí)鐘
- 01-10java基于poi導(dǎo)出excel透視表代碼實(shí)例
- 01-10java實(shí)現(xiàn)液晶數(shù)字字體顯示當(dāng)前時(shí)間
- 01-10基于Java驗(yàn)證jwt token代碼實(shí)例
- 01-10Java動(dòng)態(tài)顯示當(dāng)前日期和時(shí)間
- 01-10淺談Java中真的只有值傳遞么
隨機(jī)閱讀
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-10delphi制作wav文件的方法
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-10C#中split用法實(shí)例總結(jié)
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載