spring boot yaml 配置[三]
前言
我们知道java 因为历史的原因,一直有一个配置地狱的痛点。那么如何解决掉它呢?
spring boot 是一柄利器,但是呢,还是要配置。

看来配置的避免不了的了。
那么如何可以减轻这种痛苦呢?
yaml 就是一个减轻的利器。
正文
yaml 是什么呢? yaml ain't markup language
这是什么意思呢?解释的不是一门标记语言。但是无论它怎么说,他的确是作为标记语言存在的。
那么这个yaml配置放在哪里里?

放在这下面哈。
先来熟悉一下yaml语法吧,这里只是做一些我自己经验。
下面是菜鸟的,我认为起码应该熟悉这个。
基本语法
大小写敏感
使用缩进表示层级关系
缩进不允许使用tab,只允许空格
缩进的空格数不重要,只要相同层级的元素左对齐即可
'#'表示注释
数据类型
YAML 支持以下几种数据类型:
对象:键值对的集合,又称为映射(mapping)/ 哈希(hashes) / 字典(dictionary)
数组:一组按次序排列的值,又称为序列(sequence) / 列表(list)
纯量(scalars):单个的、不可再分的值
1.key:value
string 类型默认不用加"" 或者 ''
这种类型的话:"" 会转义,'' 不会转义。
- 对象的时候:
friend:
lastname: bob
firstname: z
如果是简单的对象建议这样写:friend:{ lastname:bob,firstname:z}
2.这里介绍一下数组,支持两种写法:
sex
- boy
- girl
如果是简单的数组。那么我建议这样写: sex:[boy,girl]
yaml 读取

yaml 除了这样写能让spring boot读取到,这是系统自动会去读取server。
那么我们自己定义的配置该如何读取?
person 类
package com.axm.demo.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* @ConfigurationProperties
*/
@Component
@ConfigurationProperties(prefix ="person")
public class Person {
private String lastName;
private Integer age;
private boolean boss;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
public String getLastName() {
return lastName;
}
@Override
public String toString() {
return "Person{" +
"lastName='" + lastName + '\'' +
", age=" + age +
", boss=" + boss +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public boolean isBoss() {
return boss;
}
public void setBoss(boolean boss) {
this.boss = boss;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Map<String, Object> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public List<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
}
dog 类
package com.axm.demo.bean;
public class Dog {
private String name;
private Integer ages;
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", ages=" + ages +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAges() {
return ages;
}
public void setAges(Integer ages) {
this.ages = ages;
}
}
那么在yml中这样写:
person:
lastName: zhangsan
age: 18
boss: false
birth: 2017/12/12
maps: {k1: v1,k2: 12}
lists:
- lisi
- zhaolu
dog:
name: 小狗
这里需要配置的是person中增加注解:
@Component
@ConfigurationProperties(prefix ="person")
pom.xml 中增加:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
测试其是否配置成功:
package com.axm.demo;
import com.axm.demo.bean.Person;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
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
class DemoApplicationTests {
@Autowired
Person person;
@Test
void contextLoads() {
System.out.println(person);
}
}
spring boot yaml 配置[三]的更多相关文章
- 转:spring boot log4j2配置(使用log4j2.yml文件)---YAML 语言教程
转:spring boot log4j2配置(使用log4j2.yml文件) - CSDN博客http://blog.csdn.net/ClementAD/article/details/514988 ...
- Spring Boot 系列(三)属性配置&自定义属性配置
在使用spring boot过程中,可以发现项目中只需要极少的配置就能完成相应的功能,这归功于spring boot中的模块化配置,在pom.xml中依赖的每个Starter都有默认配置,而这些默认配 ...
- Springboot 系列(三)Spring Boot 自动配置原理
注意:本 Spring Boot 系列文章基于 Spring Boot 版本 v2.1.1.RELEASE 进行学习分析,版本不同可能会有细微差别. 前言 关于配置文件可以配置的内容,在 Spring ...
- Spring Boot教程(三十二)多数据源配置与使用
之前在介绍使用JdbcTemplate和Spring-data-jpa时,都使用了单数据源.在单数据源的情况下,Spring Boot的配置非常简单,只需要在application.propertie ...
- Spring Boot教程(三十二)多数据源配置与使用(1)
之前在介绍使用JdbcTemplate和Spring-data-jpa时,都使用了单数据源.在单数据源的情况下,Spring Boot的配置非常简单,只需要在application.propertie ...
- 玩转spring boot——properties配置
前言 在以往的java开发中,程序员最怕大量的配置,是因为配置一多就不好统一管理,经常出现找不到配置的情况.而项目中,从开发测试环境到生产环境,往往需要切换不同的配置,如测试数据库连接换成生产数据库连 ...
- Spring boot将配置属性注入到bean类中
一.@ConfigurationProperties注解的使用 看配置文件,我的是yaml格式的配置: // file application.yml my: servers: - dev.bar.c ...
- Spring boot将配置属性注入到bean 专题
https://blog.csdn.net/wangmx1993328/article/details/81002901 Error starting ApplicationContext. To d ...
- Spring Boot自动配置原理(转)
第3章 Spring Boot自动配置原理 3.1 SpringBoot的核心组件模块 首先,我们来简单统计一下SpringBoot核心工程的源码java文件数量: 我们cd到spring-boot- ...
- Spring Boot自动配置原理与实践(二)
前言 在之前的博文(Spring Boot自动配置原理与实践(一))中,已经介绍了Spring boot的自动配置的相关原理与概念,本篇主要是对自动配置的实践,即自定义Starter,对原理与概念加深 ...
随机推荐
- Java 练习题(类+调用方法)
1 /* 2 * 3 * 定义一个 PassObject,在类中定义一个方法printAress(),该方法的定义如下: 4 * public void printAreas(Circle c,int ...
- Java与sql中的字符串表示
在 Java 中,双引号 "" 用于表示字符串字面量,而单引号 '' 用于表示字符字面量.这意味着在 Java 中,您可以使用双引号来包围包含任意数量字符的字符串,包括零个字符(空 ...
- Ansible 基础入门
2)Ansible 介绍 Ansible 基本概念 Ansible 是一种自动化运维工具,基于 Paramiko 开发的,并且基于模块化工作,Ansible 是一种集成 IT 系统的配置管理.应用部署 ...
- Github登录 2FA(Two-Factor Authentication/两因素认证) 浏览器插件-已验证
Github登录 2FA(Two-Factor Authentication/两因素认证) 浏览器插件-已验证 chrome 装下这个扩展 身份验证器 https://chromewebstore.g ...
- ECharts 中国地图 vue
<template> <div> <div id="china_map_box"> <div id="china_map&quo ...
- StatefulSet是怎样实现的
StatefulSet是Kubernetes中用于管理有状态应用的标准实现.与Deployment不同,StatefulSet为每个Pod提供了一个唯一的.稳定的网络标识符,并且Pod的启动和停止顺序 ...
- 【深度学习】批量归一化 BatchNormalization
一.背景 机器学习的本质是对物理世界进行建模,做的就是拟合数据分布. 但是在模型训练过程中,神经网络参数不断更新,导数中间层的数据分布频繁地变化(内部协变量偏移),不利于网络参数 ...
- IIS 修改配置 进行性能优化
1.修改线程池队列长度和启动模式 2.修改线程池最大工作进程数 --设置为0 目的是根据服务器核数 匹配最佳线程数 3.站点高级设置开启预加载
- 记一次配置mybatis plus报错有感
参考,欢迎点击原文:https://blog.csdn.net/wwrzyy/article/details/86034458(问题原因) https://www.jianshu.com/p/28d6 ...
- String类为什么要用final修饰?
final修饰符的意义? https://www.cnblogs.com/loren-Yang/p/13380318.html String类被实现的目标是什么? 效率和安全 如何实现期望? 参考文献 ...