一、环境

1.1、Idea 2020.1

1.2、JDK 1.8

二、目的

通过yaml文件配置spring boot 属性文件

三、步骤

3.1、点击File -> New Project -> Spring Initializer,点击next

3.2、在对应地方修改自己的项目信息

3.3、选择Web依赖,选中Spring Web。可以选择Spring Boot版本,本次默认为2.2.6,点击Next

3.4、编辑工程名和项目路径,确定后点击Finish完成

3.5、项目结构

四、添加测试方法

4.1、新建UserController实体类

package org.ouyushan.springboot.configuration.properties.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*; import java.util.HashMap; /**
* @Description:
* @Author: ouyushan
* @Email: ouyushan@hotmail.com
* @Date: 2020/4/27 13:34
*/ @RestController
@RequestMapping("/api/user")
public class UserController { @Value(value = "${ouyushan.secret}")
private String secret; @Value(value = "${ouyushan.number}")
private int number; @Value(value = "${ouyushan.desc}")
private String desc; @GetMapping("/")
public String hello() {
return "Hello World Based On Spring-Boot";
} // @RequestParam 简单类型的绑定
@GetMapping("/getUser")
public HashMap<String, Object> getUser(@RequestParam String username) {
HashMap<String, Object> map = new HashMap<>();
map.put("title", "hello world");
map.put("username", username);
map.put("secret",secret);
map.put("number",number);
map.put("desc",desc);
return map;
}
}

4.2、配置默认application.yaml

在resources/application.yaml中配置以下信息:
ouyushan:
secret: ${random.value}
number: ${random.int}
name: ouyushan.org
desc: ${ouyushan.name} is a domain name server:
port: 7070 spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: Asia/Chongqing
启动项目,访问
返回:
{
"number":1410493656,
"secret":"f99e177aeb5370bec930ec7159bb5df7",
"title":"hello world",
"username":"spring",
"desc":"ouyushan.org is a domain name"
}
 

4.3、配置resources/config/application.yaml

ouyushan:
secret: ${random.value}
number: ${random.int}
name: ouyushan.org
desc: config ${ouyushan.name} is a domain name server:
port: 9090 spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: Asia/Chongqing

启动项目,访问

返回:
{
"number":-1333749861,
"secret":"9e0107e2d0d1b91591a18e8165532201",
"title":"hello world",
"username":"spring",
"desc":"config ouyushan.org is a domain name"
}
通过对比请求地址和返回结果,发现resources/config/application.yaml中的配置内容已覆盖resources/application.yaml

五、知识点

yaml文件优先级

1. 当前目录下的一个/config子目录
2. 当前目录
3. 一个classpath下的/config包
4. classpath根路径(root) yaml中: 后面有空格 可通过@Value取值 @Value(value = "${ouyushan.secret}")
private String secret; **@ConfigurationProperties(prefix = "sample")** 读取外部配置文件中sample 开始的属性,自动映射到类中的字段,并覆盖代码中的值。
 
当同名文件存在两种格式时,先加载yaml文件再加载properties文件,会导致properties的值会覆盖yaml配置的值

Spring boot Sample 004之spring-boot-configuration-yaml的更多相关文章

  1. Spring boot Sample 009之spring-boot-web-thymeleaf

    一.环境 1.1.Idea 2020.1 1.2.JDK 1.8 二.目的 spring boot 整合thymeleaf模板开发web项目 三.步骤 3.1.点击File -> New Pro ...

  2. Spring boot Sample 012之spring-boot-web-upload

    一.环境 1.1.Idea 2020.1 1.2.JDK 1.8 二.目的 spring boot 整合web实现文件上传下载 三.步骤 3.1.点击File -> New Project -& ...

  3. Spring boot Sample 0010之spring-boot-web-freemarker

    一.环境 1.1.Idea 2020.1 1.2.JDK 1.8 二.目的 spring boot 整合freemarker模板开发web项目 三.步骤 3.1.点击File -> New Pr ...

  4. Spring boot Sample 006之spring-boot-custom-servlet

    一.环境 1.1.Idea 2020.1 1.2.JDK 1.8 二.步骤 2.1.点击File -> New Project -> Spring Initializer,点击next 2 ...

  5. Spring boot Sample 005之spring-boot-profile

    一.环境 1.1.Idea 2020.1 1.2.JDK 1.8 二.目的 通过yaml文件配置spring boot 属性文件 三.步骤 3.1.点击File -> New Project - ...

  6. spring cloud教程之使用spring boot创建一个应用

    <7天学会spring cloud>第一天,熟悉spring boot,并使用spring boot创建一个应用. Spring Boot是Spring团队推出的新框架,它所使用的核心技术 ...

  7. Spring Boot——2分钟构建spring web mvc REST风格HelloWorld

    之前有一篇<5分钟构建spring web mvc REST风格HelloWorld>介绍了普通方式开发spring web mvc web service.接下来看看使用spring b ...

  8. 【Spring】关于Boot应用中集成Spring Security你必须了解的那些事

    Spring Security Spring Security是Spring社区的一个顶级项目,也是Spring Boot官方推荐使用的Security框架.除了常规的Authentication和A ...

  9. [转]Spring Boot——2分钟构建spring web mvc REST风格HelloWorld

    Spring Boot——2分钟构建spring web mvc REST风格HelloWorld http://projects.spring.io/spring-boot/ http://spri ...

随机推荐

  1. Java——字节和字符的区别

    字节 1.bit=1  二进制数据0或1 2.byte=8bit  1个字节等于8位 存储空间的基本计量单位 3.一个英文字母=1byte=8bit 1个英文字母是1个字节,也就是8位 4.一个汉字= ...

  2. js 如何保存代码段并执行以及动态加载script

    1.模块化开发 通常使用的是 export和import 实现代码的共享和导入 2.特殊情况下需要将代码段作为参数传递 可以使用function 的toString方法将整合函数和里面的代码批量转化为 ...

  3. 【Hadoop离线基础总结】工作流调度器azkaban

    目录 Azkaban概述 工作流调度系统的作用 工作流调度系统的实现 常见工作流调度工具对比 Azkaban简单介绍 安装部署 Azkaban的编译 azkaban单服务模式安装与使用 azkaban ...

  4. Day_11【集合】扩展案例2_使用普通for循环获取集合中索引为3的元素并打印,统计集合中包含字符串"def"的数量,删除集合中的所有字符串",将集合中每个元素中的小写字母变成大写字母def",

    分析以下需求,并用代码实现 1.定义ArrayList集合,存入多个字符串"abc" "def" "efg" "def" ...

  5. Linux内核驱动学习(一)编写最简单Linux内核模块HelloWorld

    文章目录 准备工作 什么是内核模块 编写 hello.c 模块编译 相关指令 测试结果 模块加载 模块卸载 准备工作 在进行以下操作前,首先我准备了一台电脑,并且安装了虚拟机,系统是Ubuntu16. ...

  6. InnoDB的ibd数据文件为什么比data_length+index_length+data_free的总和还要大?

    问题描述: 同事在给jiradb做mysqldump时,发现dump出来的文件只有10MB左右,而ibd文件占用磁盘空间100MB左右. 最初,我们猜测可能是delete操作导致了大量的磁盘碎片,以及 ...

  7. CF-163A Substring and Subsequence 字符串DP

    Substring and Subsequence 题意 给出两个字符串s,t,求出有多少对s的子串和t的子序列相等. 思路 类似于最长公共子序列的dp数组. dp[i][j]表示s中以i为结尾的子串 ...

  8. Mysql常用sql语句(17)- left/right join 外连接

    测试必备的Mysql常用sql语句系列 https://www.cnblogs.com/poloyy/category/1683347.html 前言 外连接分为两种:left join.right ...

  9. [hdu1028]整数拆分,生成函数

    题意:给一个正整数n,求n的拆分方法数(不考虑顺序) 思路:不妨考虑用1~n来构成n.用多项式表示单个数所有能构成的数,用多项式表示,就相当于卷积运算了. 1 2 3 4 5 6 7 8 9 10 1 ...

  10. [hdu5200]离线+标记

    思路:按顺序处理,新建一堆然后向左右合并,不过巧妙地用了标记数组来记录和统计答案. #pragma comment(linker, "/STACK:10240000,10240000&quo ...