您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關如何理解SPRINGBOOT讀取PROPERTIES配置文件數據過程,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
一.使用@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=Camelcoffer.length=100.00coffer.width=80.00coffer.height=60.00coffer.contains[0]=Raincoatcoffer.contains[1]=trouserscoffer.contains[2]=hatcoffer.contains[3]=glovecoffer.contains[4]=scarfcoffer.contains[5]=hoodcoffer.fruits[0].fruitName=apricotcoffer.fruits[0].fruitColor=yellowcoffer.fruits[1].fruitName=plumcoffer.fruits[1].fruitColor=greencoffer.fruits[2].fruitName=pineapplecoffer.fruits[2].fruitColor=yellowcoffer.fruits[3].fruitName=watermeloncoffer.fruits[3].fruitColor=greencoffer.fruits[4].fruitName=strawberrycoffer.fruits[4].fruitColor=redcoffer.map.name=xiaomaocoffer.map.age=22coffer.map.gender=female
4、springbootApplicationTest
@SpringBootTestclass 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")@RestControllerpublic class SpringbootController { @Value("${coffer.brand}") private String brand; @Value("${coffer.height}") private Double height; @RequestMapping("/test") public String springbootTest() { return brand+"====="+height; }}
看完上述內容,你們對如何理解SPRINGBOOT讀取PROPERTIES配置文件數據過程有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。