How to understand the process of reading PROPERTIES configuration file data by SPRINGBOOT
Today, I will talk to you about how to understand the process of SPRINGBOOT reading PROPERTIES configuration file data, which may not be understood by many people. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.
one。 Use @ ConfigurationProperties to read
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; / / omitted the get/set method private String [] contains; private ArrayList fruits; private HashMap map;}
2 、 Fruit entity
@ Configuration@ConfigurationProperties (prefix = "coffer.fruits") @ PropertySource ("classpath:config/coffer.properties") public class Fruit {private String fruitName; private String fruitColor; / / omitted the get/set method}
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}}
Use @ Value to read
You can't use @ Value to read configuration properties in springTest, you need to put it in Controller to read
@ 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;}}
After reading the above, do you have any further understanding of how SPRINGBOOT reads PROPERTIES configuration file data? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.