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 ...
随机推荐
- Linux之RTOS学习
Linux之RTOS学习 RTOS: Real time operating system 系统选型 可选方案 RTLinux - FSMLabs, WindRiver Systems - http: ...
- direct path read temp的处理方法
Examine the SQL statement currently being run by the session experiencing waits to see what is causi ...
- jenkins+mail邮件配置
1.配置过程中出现的问题,“501 mail from address must be same as authorization user” 解决方案 2.还有一个问题,在配置jenkins的系统配 ...
- 从String.valueOf(null)说起
同学在群问String.valueOf(null)返回啥,我看了下源码,返回"null"啊, public static String valueOf(Object obj) ...
- macOS10.4后的刻盘新姿势
先sudo -s 输入密码 然后终端拖入createinstallmedia (在macOS Mojave.app显示包内容里面的resources里面的文件复制下来即可) 输入 --volume ...
- SpringBoot开发案例之整合Dubbo分布式服务
前言 在 SpringBoot 很火热的时候,阿里巴巴的分布式框架 Dubbo 不知是处于什么考虑,在停更N年之后终于进行维护了.在之前的微服务中,使用的是当当维护的版本 Dubbox,整合方式也是使 ...
- 解决win7 win10上网卡慢问题
在管理员身份运行CMD命令行,然后输入netsh interface tcp set global autotuninglevel=disable 即可.
- 【fetch跨域请求】cors
当使用fetch 发起跨域请求时,CORS(跨域资源共享Cross-origin resource sharing) 请求fetch const body = {name:"Good boy ...
- Python入门-格式化输出
需求:将用户的姓名.年龄.工作.爱好 ,然后打印成以下格式: ------------ Info of Tom ------ Name : Tom Age : 22 Job : Teacher Hob ...
- [2019BUAA软工助教]答黄杉同学
[2019BUAA软工助教]答黄杉同学 一.答黄杉同学 011-黄衫博客 我当然不否认软件工程的各种博客是有一定作用的,但是相信大多数人对诸如例会博客并没有什么热情(不过似乎也没有什么其他方法保证团队 ...