在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. gcc学习记录

    -Wall: 使输出中包含警告信息,提示一些可以避免的错误.如果没有错误,则不会输出信息. -o:后面加上可执行文件的名字.如果不加-o选项,会默认生成a.out可执行文件.举例:gcc -Wall ...

  2. 项目记事【StreamAPI】:使用 StreamAPI 简化对 Collection 的操作

    最近项目里有这么一段代码,我在做 code-review 的时候,觉得可以使用 Java8 StreamAPI 简化一下. 这里先看一下代码(不是源码,一些敏感信息被我用其他类替代了): privat ...

  3. [HDU3516] Tree Construction [四边形不等式dp]

    题面: 传送门 思路: 这道题有个结论: 把两棵树$\left[i,k\right]$以及$\left[k+1,j\right]$连接起来的最小花费是$x\left[k+1\right]-x\left ...

  4. 字符串函数 (strfun)

    字符串函数 (strfun) 题目描述 两个等长的由大写英文字母构成的字符串a和b,从a中选择连续子串x,从b中选出连续子串y.子串x与子串y的长度相等. 定义函数f(x,y)为满足条件xi=yi(1 ...

  5. easyUI 接收Spring Mvc中@ResponseBody中文乱码解决

    接触springMVC不够深入,乱码困扰我到深夜,特此留下记忆: @responsebody默认滴是ISO-8859-1 Controller注解参数 @ResponseBody 标注后返回Strin ...

  6. pat 甲级 1064. Complete Binary Search Tree (30)

    1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

  7. pat 团体天梯赛 L2-010. 排座位

    L2-010. 排座位 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 布置宴席最微妙的事情,就是给前来参宴的各位宾客安排座位. ...

  8. 【虚拟机】主机与VMware虚拟机通信(XP版)(转)

    一.与主机共享ADSL链接/无线网络(虚拟机内可上网) 安装虚拟机后,在网络链接下除了本地链接外,会出现两个新的链接,分别是VMware Network Adapter VMnet1和VMware N ...

  9. java中 文件压缩处理

    public static void main(String[] args) throws IOException { File file=new File("./mhxx_configs. ...

  10. QUICK START GIT

    install git at you laptop https://git-scm.com/downloads config git at you laptop git config --global ...