在spring boot中,简单几步,读取配置文件(application.yml)中各种不同类型的属性值:

1、引入依赖:

  1. <!-- 支持 @ConfigurationProperties 注解 -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-configuration-processor</artifactId>
  5. <optional>true</optional>
  6. </dependency>

2、配置文件(application.yml)中配置各个属性的值:

  1. myProps: #自定义的属性和值
  2. simpleProp: simplePropValue
  3. arrayProps: 1,2,3,4,5
  4. listProp1:
  5. - name: abc
  6. value: abcValue
  7. - name: efg
  8. value: efgValue
  9. listProp2:
  10. - config2Value1
  11. - config2Vavlue2
  12. mapProps:
  13. key1: value1
  14. key2: value2

3、创建一个bean来接收配置信息:

  1. @Component
  2. @ConfigurationProperties(prefix="myProps") //接收application.yml中的myProps下面的属性
  3. public class MyProps {
  4. private String simpleProp;
  5. private String[] arrayProps;
  6. private List<Map<String, String>> listProp1 = new ArrayList<>(); //接收prop1里面的属性值
  7. private List<String> listProp2 = new ArrayList<>(); //接收prop2里面的属性值
  8. private Map<String, String> mapProps = new HashMap<>(); //接收prop1里面的属性值
  9. public String getSimpleProp() {
  10. return simpleProp;
  11. }
  12. //String类型的一定需要setter来接收属性值;maps, collections, 和 arrays 不需要
  13. public void setSimpleProp(String simpleProp) {
  14. this.simpleProp = simpleProp;
  15. }
  16. public List<Map<String, String>> getListProp1() {
  17. return listProp1;
  18. }
  19. public List<String> getListProp2() {
  20. return listProp2;
  21. }
  22. public String[] getArrayProps() {
  23. return arrayProps;
  24. }
  25. public void setArrayProps(String[] arrayProps) {
  26. this.arrayProps = arrayProps;
  27. }
  28. public Map<String, String> getMapProps() {
  29. return mapProps;
  30. }
  31. public void setMapProps(Map<String, String> mapProps) {
  32. this.mapProps = mapProps;
  33. }
  34. }

启动后,这个bean里面的属性就会自动接收配置的值了。

4、单元测试用例:

  1. @Autowired
  2. private MyProps myProps;
  3. @Test
  4. public void propsTest() throws JsonProcessingException {
  5. System.out.println("simpleProp: " + myProps.getSimpleProp());
  6. System.out.println("arrayProps: " + objectMapper.writeValueAsString(myProps.getArrayProps()));
  7. System.out.println("listProp1: " + objectMapper.writeValueAsString(myProps.getListProp1()));
  8. System.out.println("listProp2: " + objectMapper.writeValueAsString(myProps.getListProp2()));
  9. System.out.println("mapProps: " + objectMapper.writeValueAsString(myProps.getMapProps()));
  10. }

测试结果:

  1. simpleProp: simplePropValue
  2. arrayProps: ["1","2","3","4","5"]
  3. listProp1: [{"name":"abc","value":"abcValue"},{"name":"efg","value":"efgValue"}]
  4. listProp2: ["config2Value1","config2Vavlue2"]
  5. mapProps: {"key1":"value1","key2":"value2"}

spring boot 读取配置文件(application.yml)中的属性值的更多相关文章

  1. Spring Boot 的配置文件application.properties

    Spring Boot 中的application.properties 是一个全局的配置文件,放在src/main/resources 目录下或者类路径的/config下. 作为全局配置文件的app ...

  2. 一:Spring Boot 的配置文件 application.properties

    Spring Boot 的配置文件 application.properties 1.位置问题 2.普通的属性注入 3.类型安全的属性注入 1.位置问题 当我们创建一个 Spring Boot 工程时 ...

  3. 是时候搞清楚 Spring Boot 的配置文件 application.properties 了!

    在 Spring Boot 中,配置文件有两种不同的格式,一个是 properties ,另一个是 yaml . 虽然 properties 文件比较常见,但是相对于 properties 而言,ya ...

  4. spring boot读取配置文件

    一.springboot配置文件 核心配置文件和自定义配置文件.核心配置文件是指在resources根目录下的application.properties或application.yml配置文     ...

  5. 第二篇:彻底搞清楚 Spring Boot 的配置文件 application.properties

    前言 在Spring Boot中,配置文件有两种不同的格式,一个是properties,另一个是yaml. 虽然properties文件比较常见,但是相对于properties而言,yaml更加简洁明 ...

  6. IDEA开发spring boot应用时 application.yml 或 application.properties 自定义属性提示

    在使用spring boot开发过程中,经常会定义一些应用自己的属性,直接写到application配置文件中使用@Value注解进行使用,这样使用也没有什么问题.不过我认为更优雅的方式是定义自己的属 ...

  7. spring boot 读取配置文件信息

    1.读取application.properties @Component @ConfigurationProperties(prefix="images.product.column&qu ...

  8. Spring Boot读取配置文件的几种方式

    Spring Boot获取文件总的来说有三种方式,分别是@Value注解,@ConfigurationProperties注解和Environment接口.这三种注解可以配合着@PropertySou ...

  9. springboot工程读取配置文件application.yml的写法

    现在流行springboot框架的项目,里面的默认配置文件为application.yml,我们怎样读取这个配置文件呢? 先贴上我得配置文件吧 目录结构 里面内容 1 写读取配置文件的工具类 @Con ...

随机推荐

  1. 利用python爬取58同城简历数据

    利用python爬取58同城简历数据 利用python爬取58同城简历数据 最近接到一个工作,需要获取58同城上面的简历信息(http://gz.58.com/qzyewu/).最开始想到是用pyth ...

  2. [oldboy-django][2深入django]分页功能

    1 django自带分页 1.1 分页模板 <!DOCTYPE html> <html lang="en"> <head> <meta c ...

  3. poj3083 Children of the Candy Corn BFS&&DFS

    Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11215   Acce ...

  4. NOIP试题解析

    NOIP试题解析           by QTY_YTQ noip2010关押罪犯(并查集) 题意是有n个罪犯关在两个监狱中,其中有m对罪犯有仇恨关系,如果有仇恨的罪犯关在一起会产生一定影响力的事件 ...

  5. 【HDU 2087 剪花布条】

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  6. hdu 5669 Road

    题目大意 \(n\)个点,\(m\)次连边 每次连边为\((a\sim b)\leftrightarrow (c\sim d),cost=w\) 即在\((a-b)\)区间内的点向\((c-d)\)区 ...

  7. hash function 字符串哈希函数

    #include <stdio.h> int hash(const char *str) { ; ;;i++) { if (str[i] == '\0') break; sum += (( ...

  8. CodeVS 1503 愚蠢的宠物

    题目描述 Description 大家都知道,sheep有两只可爱的宠物(一只叫神牛,一只叫神菜).有一天,sheep带着两只宠物到狗狗家时,这两只可爱的宠物竟然迷路了…… 狗狗的家因为常常遭到猫猫的 ...

  9. Javascript的SEO优化技巧

    原文发布时间为:2010-10-22 -- 来源于本人的百度文章 [由搬家工具导入] 1.外部崁入javascript在撰写一些比较复杂的网页特效,如下拉式选单等,会产生大量的javascript码, ...

  10. ng-show和ng-hide的进阶应用

    在数据列表渲染的时候,我们可能不仅有一层数据,可能是三级甚至更多,如果我们要显示或者隐藏对应的数据,那么就需要给数据进行子scope的绑定.如下 html <div ng-app="d ...