SpringBoot之通过yaml绑定注入数据
依赖包:
- <!--配置文件注解提示包-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-configuration-processor</artifactId>
- <optional>true</optional>
- </dependency>
JavaBean:(此处使用lombok,可省略setter、getter等方法)
- package org.springboot.model;
- import lombok.Data;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.stereotype.Component;
- import java.util.Date;
- import java.util.Map;
- /**
- * @Description: 通过yaml绑定,注入数据的模型
- **/
- @Data
- @Component
- @ConfigurationProperties(prefix = "teather")
- public class Teather {
- @Value("小红") //单个赋值,优先级低
- private String name;
- // @Value("${teather.age}") // 使用SpringEl读取配置文件中的值并注入
- private String age;
- private Date birthday;
- private Boolean gender;
- private String[] hobbies; //集合处理方式和数组相同
- // {省:江苏,市:南京}
- private Map<String, Object> location;
- }
application.yml
- # 通过yaml绑定,注入数据
- teather:
- name: Cate
- age:
- birthday: //
- gender: true
- hobbies: [唱歌,跳舞]
- location: {Province: "江苏",City: "南京"}
已缩进来区分同级,并且冒号后必须有空格。
测试代码:
- package org.springboot;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springboot.model.Teather;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.test.context.junit4.SpringRunner;
- @RunWith(SpringRunner.class)
- @SpringBootTest
- public class DemoApplicationTests {
- @Autowired
- Teather teather;
- //通过yaml绑定,注入数据
- @Test
- public void testTeather() {
- System.out.println(teather);
- }
- }
执行结果
- Teather(name=Cate, age=29, birthday=Mon Jan 16 00:00:00 CST 1989, gender=true, hobbies=[唱歌, 跳舞], location={Province=江苏, City=南京})
此处绑定注入类型分为批量注入和单个注入,批量注入的优先级较高,两种方式的比较如下图:
SpringBoot之通过yaml绑定注入数据的更多相关文章
- springboot----四、yaml配置注入
四.yaml配置注入 4.1.配置文件 SpringBoot使用一个全局的配置文件 , 配置文件名称是固定的 application.properties 语法结构 :key=value applic ...
- SpringBoot系列之YAML配置用法
1.全局配置 SpringBoot的全局配置文件有两种: application.properties application.yml 配置文件的作用:修改SpringBoot自动配置的默认值,主要是 ...
- SpringBoot之Spring@Value属性注入使用详解
在使用Spring框架的项目中,@Value是使用比较频繁的注解之一,它的作用是将配置文件中key对应的值赋值给它标注的属性.在日常使用中我们常用的功能都比较简单,本篇文章系统的带大家来了解一下@Va ...
- SpringBoot 03_利用FastJson返回Json数据
自上一节:SpringBoot 02_返回json数据,可以返回json数据之后,由于有些人习惯于不同的Json框架,比如fastjson,这里介绍一下如何在SpringBoot中集成fastjson ...
- winform快速开发平台 -> 快速绑定ComboBox数据控件
通常我们在处理编辑窗体时.往往会遇到数据绑定.例如combobox控件绑定数据字典可能是我们经常用到的.然而在我的winform快速开发平台中我是如何处理这个频繁的操作呢? 首先,我们要绑定combo ...
- 背水一战 Windows 10 (20) - 绑定: DataContextChanged, UpdateSourceTrigger, 对绑定的数据做自定义转换
[源码下载] 背水一战 Windows 10 (20) - 绑定: DataContextChanged, UpdateSourceTrigger, 对绑定的数据做自定义转换 作者:webabcd 介 ...
- 绑定: DataContextChanged, UpdateSourceTrigger, 对绑定的数据做自定义转换
介绍背水一战 Windows 10 之 绑定 DataContextChanged - FrameworkElement 的 DataContext 发生变化时触发的事件 UpdateSourceTr ...
- c#中DropDownList控件绑定枚举数据
c# asp.net 中DropDownList控件绑定枚举数据 1.枚举(enum)代码: private enum heros { 德玛 = , 皇子 = , 大头 = , 剑圣 = , } 如果 ...
- WPF——绑定数据库数据(Listview)
一.首先先画一个窗体,放进一个Listview 然后给每列起好名字,并且绑定的数据是临时表的列名 二.造一个临时表用来存储数据,并且将扔进去的Listview绑定到这个临时表DataTable上面 p ...
随机推荐
- Attribute "resultType" must be declared for element type "insert"或"update"
Attribute "resultType" must be declared for element type "insert"或"update&q ...
- 比起Windows,怎样解读Linux的文件系统与目录结构?
比起Windows,怎样解读Linux的文件系统与目录结构? Linux 和Windows的文件系统有些不同,在学习使用 Linux 之前,若能够了解这些不同,会有助于后续学习. 本文先对Window ...
- C#获取指定IP地址的数据库所有数据库实例名
/// <summary> /// 获取指定IP地址的数据库所有数据库实例名. /// </summary> /// <param name="ip" ...
- 朱晔和你聊Spring系列S1E3:Spring咖啡罐里的豆子
标题中的咖啡罐指的是Spring容器,容器里装的当然就是被称作Bean的豆子.本文我们会以一个最基本的例子来熟悉Spring的容器管理和扩展点. 阅读PDF版本 为什么要让容器来管理对象? 首先我们来 ...
- 根据指定条件使CheckBox 无法选中
var trList = $("#tab1").children("tr")for (var i=0;i<trList.length;i++) {var ...
- Python全栈开发之路 【第十八篇】:Ajax技术
Ajax技术 Ajax = 异步 JavaScript 和 XML. Ajax 是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术. 1.jQuery的load()方法 jQuery loa ...
- 容器互联(linking)
容器互联(linking)是一种让多个容器中的应用进行快速交互的方式. 它会在源和接受容器中间创建连接关系,接受容器可以通过容器名快速访问到源容器而不用指出具体的IP地址.
- 使用 Markdown编辑
作用: 学习笔记,整理日志, 发布日记,杂文,所见所想 撰写发布技术文稿(代码支持) 撰写发布学术论文(LaTeX 公式支持) sublime text3插件 输入 Shift + Ctrl + P, ...
- hdu3790 dijkstra+堆优化
题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=3790 分析:dijkstra没有优化的话,复杂度是n*n,优化后的复杂度是m*logm,n是顶点数,m ...
- urllib库
python内置的最基本的HTTP请求库,有以下四个模块: urllib.request 请求模块 urllib.error 异常处理模块 urllib.parse url解析模块 urllib.ro ...