SPRINGBOOT讀取PROPERTIES配置文件數(shù)據(jù)過程詳解
這篇文章主要介紹了SPRINGBOOT讀取PROPERTIES配置文件數(shù)據(jù)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
一.使用@ConfigurationProperties來讀取
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來讀取
在springTest中無法使用@Value來讀取配置屬性,需要放到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;
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot yaml語法與數(shù)據(jù)讀取操作詳解
- SpringBoot讀取自定義配置文件方式(properties,yaml)
- 解決SpringBoot application.yaml文件配置schema 無法執(zhí)行sql問題
- SpringBoot+thymeleaf+Echarts+Mysql 實現(xiàn)數(shù)據(jù)可視化讀取的示例
- SpringBoot如何讀取配置文件中的數(shù)據(jù)到map和list
- springboot使用AOP+反射實現(xiàn)Excel數(shù)據(jù)的讀取
- 解決springboot利用ConfigurationProperties注解配置數(shù)據(jù)源無法讀取配置信息問題
- SpringBoot讀取properties或者application.yml配置文件中的數(shù)據(jù)
- springboot openfeign從JSON文件讀取數(shù)據(jù)問題
- springboot讀取application.yaml文件數(shù)據(jù)的方法
相關(guān)文章
一起來學(xué)習(xí)Java IO的轉(zhuǎn)化流
這篇文章主要為大家詳細(xì)介紹了Java IO的轉(zhuǎn)化流,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03
Springmvc如何實現(xiàn)向前臺傳遞數(shù)據(jù)
這篇文章主要介紹了Springmvc如何實現(xiàn)向前臺傳遞數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07
Spring實戰(zhàn)之Bean的作用域request用法分析
這篇文章主要介紹了Spring實戰(zhàn)之Bean的作用域request用法,結(jié)合實例形式分析了spring中Bean的request作用域相關(guān)使用技巧與操作注意事項,需要的朋友可以參考下2019-11-11
java實現(xiàn)301跳轉(zhuǎn)和重定向的方法
301跳轉(zhuǎn)和重定向是做項目的時候經(jīng)常需要用到的,本文給大家分享的是在java中301跳轉(zhuǎn)和重定向的方法,需要的小伙伴參考下吧。2015-03-03

