依赖包:

  1. <!--配置文件注解提示包-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-configuration-processor</artifactId>
  5. <optional>true</optional>
  6. </dependency>

JavaBean:(此处使用lombok,可省略setter、getter等方法)

  1. package org.springboot.model;
  2.  
  3. import lombok.Data;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.boot.context.properties.ConfigurationProperties;
  6. import org.springframework.stereotype.Component;
  7.  
  8. import java.util.Date;
  9. import java.util.Map;
  10.  
  11. /**
  12. * @Description: 通过yaml绑定,注入数据的模型
  13. **/
  14.  
  15. @Data
  16. @Component
  17. @ConfigurationProperties(prefix = "teather")
  18. public class Teather {
  19. @Value("小红") //单个赋值,优先级低
  20. private String name;
  21. // @Value("${teather.age}") // 使用SpringEl读取配置文件中的值并注入
  22. private String age;
  23. private Date birthday;
  24. private Boolean gender;
  25. private String[] hobbies; //集合处理方式和数组相同
  26. // {省:江苏,市:南京}
  27. private Map<String, Object> location;
  28.  
  29. }

application.yml

  1. # 通过yaml绑定,注入数据
  2. teather:
  3. name: Cate
  4. age:
  5. birthday: //
  6. gender: true
  7. hobbies: [唱歌,跳舞]
  8. location: {Province: "江苏",City: "南京"}

已缩进来区分同级,并且冒号后必须有空格。

测试代码:

  1. package org.springboot;
  2.  
  3. import org.junit.Test;
  4. import org.junit.runner.RunWith;
  5. import org.springboot.model.Teather;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.boot.test.context.SpringBootTest;
  8. import org.springframework.test.context.junit4.SpringRunner;
  9.  
  10. @RunWith(SpringRunner.class)
  11. @SpringBootTest
  12. public class DemoApplicationTests {
  13. @Autowired
  14. Teather teather;
  15.  
  16. //通过yaml绑定,注入数据
  17. @Test
  18. public void testTeather() {
  19. System.out.println(teather);
  20. }
  21.  
  22. }

执行结果

  1. Teather(name=Cate, age=29, birthday=Mon Jan 16 00:00:00 CST 1989, gender=true, hobbies=[唱歌, 跳舞], location={Province=江苏, City=南京})

此处绑定注入类型分为批量注入和单个注入,批量注入的优先级较高,两种方式的比较如下图:

SpringBoot之通过yaml绑定注入数据的更多相关文章

  1. springboot----四、yaml配置注入

    四.yaml配置注入 4.1.配置文件 SpringBoot使用一个全局的配置文件 , 配置文件名称是固定的 application.properties 语法结构 :key=value applic ...

  2. SpringBoot系列之YAML配置用法

    1.全局配置 SpringBoot的全局配置文件有两种: application.properties application.yml 配置文件的作用:修改SpringBoot自动配置的默认值,主要是 ...

  3. SpringBoot之Spring@Value属性注入使用详解

    在使用Spring框架的项目中,@Value是使用比较频繁的注解之一,它的作用是将配置文件中key对应的值赋值给它标注的属性.在日常使用中我们常用的功能都比较简单,本篇文章系统的带大家来了解一下@Va ...

  4. SpringBoot 03_利用FastJson返回Json数据

    自上一节:SpringBoot 02_返回json数据,可以返回json数据之后,由于有些人习惯于不同的Json框架,比如fastjson,这里介绍一下如何在SpringBoot中集成fastjson ...

  5. winform快速开发平台 -> 快速绑定ComboBox数据控件

    通常我们在处理编辑窗体时.往往会遇到数据绑定.例如combobox控件绑定数据字典可能是我们经常用到的.然而在我的winform快速开发平台中我是如何处理这个频繁的操作呢? 首先,我们要绑定combo ...

  6. 背水一战 Windows 10 (20) - 绑定: DataContextChanged, UpdateSourceTrigger, 对绑定的数据做自定义转换

    [源码下载] 背水一战 Windows 10 (20) - 绑定: DataContextChanged, UpdateSourceTrigger, 对绑定的数据做自定义转换 作者:webabcd 介 ...

  7. 绑定: DataContextChanged, UpdateSourceTrigger, 对绑定的数据做自定义转换

    介绍背水一战 Windows 10 之 绑定 DataContextChanged - FrameworkElement 的 DataContext 发生变化时触发的事件 UpdateSourceTr ...

  8. c#中DropDownList控件绑定枚举数据

    c# asp.net 中DropDownList控件绑定枚举数据 1.枚举(enum)代码: private enum heros { 德玛 = , 皇子 = , 大头 = , 剑圣 = , } 如果 ...

  9. WPF——绑定数据库数据(Listview)

    一.首先先画一个窗体,放进一个Listview 然后给每列起好名字,并且绑定的数据是临时表的列名 二.造一个临时表用来存储数据,并且将扔进去的Listview绑定到这个临时表DataTable上面 p ...

随机推荐

  1. Attribute "resultType" must be declared for element type "insert"或"update"

    Attribute "resultType" must be declared for element type "insert"或"update&q ...

  2. 比起Windows,怎样解读Linux的文件系统与目录结构?

    比起Windows,怎样解读Linux的文件系统与目录结构? Linux 和Windows的文件系统有些不同,在学习使用 Linux 之前,若能够了解这些不同,会有助于后续学习. 本文先对Window ...

  3. C#获取指定IP地址的数据库所有数据库实例名

    /// <summary> /// 获取指定IP地址的数据库所有数据库实例名. /// </summary> /// <param name="ip" ...

  4. 朱晔和你聊Spring系列S1E3:Spring咖啡罐里的豆子

    标题中的咖啡罐指的是Spring容器,容器里装的当然就是被称作Bean的豆子.本文我们会以一个最基本的例子来熟悉Spring的容器管理和扩展点. 阅读PDF版本 为什么要让容器来管理对象? 首先我们来 ...

  5. 根据指定条件使CheckBox 无法选中

    var trList = $("#tab1").children("tr")for (var i=0;i<trList.length;i++) {var ...

  6. Python全栈开发之路 【第十八篇】:Ajax技术

    Ajax技术 Ajax = 异步 JavaScript 和 XML. Ajax 是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术. 1.jQuery的load()方法 jQuery loa ...

  7. 容器互联(linking)

    容器互联(linking)是一种让多个容器中的应用进行快速交互的方式. 它会在源和接受容器中间创建连接关系,接受容器可以通过容器名快速访问到源容器而不用指出具体的IP地址.

  8. 使用 Markdown编辑

    作用: 学习笔记,整理日志, 发布日记,杂文,所见所想 撰写发布技术文稿(代码支持) 撰写发布学术论文(LaTeX 公式支持) sublime text3插件 输入 Shift + Ctrl + P, ...

  9. hdu3790 dijkstra+堆优化

    题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=3790 分析:dijkstra没有优化的话,复杂度是n*n,优化后的复杂度是m*logm,n是顶点数,m ...

  10. urllib库

    python内置的最基本的HTTP请求库,有以下四个模块: urllib.request 请求模块 urllib.error 异常处理模块 urllib.parse url解析模块 urllib.ro ...