十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
今天就跟大家聊聊有关如何理解SPRINGBOOT读取PROPERTIES配置文件数据过程,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
创新互联是一家专注于成都做网站、成都网站制作与策划设计,马鞍山网站建设哪家好?创新互联做网站,专注于网站建设十多年,网设计领域的专业建站公司;建站业务涵盖:马鞍山等地区。马鞍山做网站价格咨询:18982081108
一.使用@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
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配置文件数据过程有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。