在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. 监视网络接口TCP状态信息数据有多种工具或命令。下面举例一些:

    nstat命令 nstat kernel ======= ss -s == netstat -i netstat -s ip -s link sar -n DEV 1

  2. Callable、Future、FutureTask_笔记

    参考:http://blog.csdn.net/javazejian/article/details/50896505 1.Callable<V>接口 Runnable接口 public ...

  3. 【bzoj1822】[JSOI2010]Frozen Nova 冷冻波 计算几何+二分+网络流最大流

    题目描述 WJJ喜欢“魔兽争霸”这个游戏.在游戏中,巫妖是一种强大的英雄,它的技能Frozen Nova每次可以杀死一个小精灵.我们认为,巫妖和小精灵都可以看成是平面上的点. 当巫妖和小精灵之间的直线 ...

  4. SPOJ COT2 - Count on a tree II(LCA+离散化+树上莫队)

    COT2 - Count on a tree II #tree You are given a tree with N nodes. The tree nodes are numbered from  ...

  5. [luoguP1975] [国家集训队]排队(分块)

    传送门 直接暴力分块,然后在每一个块内排序. 查询时可以在每一个块内二分. #include <cmath> #include <cstdio> #include <io ...

  6. location.origin兼容

    if (!window.location.origin) { window.location.origin = window.location.protocol + "//" + ...

  7. pat 甲级 1080. Graduate Admission (30)

    1080. Graduate Admission (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...

  8. Bzoj1195 [HNOI2006]最短母串 [状态压缩]

    Time Limit: 10 Sec  Memory Limit: 32 MBSubmit: 1304  Solved: 439 Description 给定n个字符串(S1,S2,„,Sn),要求找 ...

  9. 【CF1015F】Bracket Substring(字符串DP)

    题意:给定一个只由左右括号组成的字符串s,问长度为2*n的包含它的合法括号序列方案数,答案对1e9+7取模 1≤n≤100,1≤|s|≤200 思路:暴力预处理出s的每个前缀[0..i]后加左右括号分 ...

  10. my conclusion about time planing and requirement changes.

    How to estimate time when requirement changes or is added: my principle: never say so easy, at least ...