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 ...
随机推荐
- jenkins使用4----git maven工具连接
搭建完git服务器 将jenkins服务器的的公钥传到git服务器的/home/git/.ssh的authorized_keys文件下 ssh端口2994 创建工程 配置完maven发现创建项目没有m ...
- Burp Suite学习之Intruder的4种攻击模式
burp suit的intruder攻击共有四种模式,如图所示,下面分别讲讲这四种模式的使用方法和场景. 一 .Sniper模式 Sniper模式使用一组payload集合,它一次只使用一个paylo ...
- Maven学习第2期---Maven安装配置
一.Maven介绍 1.1 何为Maven Maven这个词可以翻译为"知识的积累",也可以翻译为"专家"或"内行".Maven是一个跨平台 ...
- UWP 自定义控件:了解模板化控件 系列文章
UWP自定义控件的入门文章 [UWP 自定义控件]了解模板化控件(1):基础知识 [UWP 自定义控件]了解模板化控件(2):模仿ContentControl [UWP 自定义控件]了解模板化控件(2 ...
- vscode中php断点调试方法!
一.PHP的代码断点调试 1.打开vscode的首选项设置,添加"php.validate.executablePath": "D:\\newXampp\\php\\ph ...
- 关于oracle设置主键自增的问题
关于orcale设置主键自增的问题 关于主键Oracle中并没有提供一个直接的语句设置,对于这个oralce一般都是用序列和触发器来实现 一下又两种方法来实现 一 ,不使用触发器 创建序列: crea ...
- Day15 Python基础之logging模块(十三)
参考源:http://www.cnblogs.com/yuanchenqi/articles/5732581.html logging模块 (****重点***) 一 (简单应用) import lo ...
- Ajax中文乱码的解决
网上有很多解决Ajax中文乱码的例子,昨晚弄了很久,最终确定一种“确实”有效地方法.首先我有必要说明一下我遇到的情况:有一个注册页面,注册用户填完信息并提交后,页面获得信息并通过java servle ...
- MySQL中关于数据类型指定宽度之后的情况
概述 MySQL有很多种数据类型,最常用的就是int,char,varchar,这些类型在创建表的时候都可以指定该字段的宽度,方法是在类型后面加一个括号,括号中写宽度就可以了. 但是,在指定宽度之后, ...
- Column 'parent_id' specified twice
Hibernate Column 'parent_id' specified twice问题解决--insertable = false, updatable = false的使用 - shendeg ...