SpringBoot学习笔记(2) - YAML配置文件

一.YAML 语法

SpringBoot使用一个全局的配置文件,配置文件名是固定的;

  • application.properties
  • application.yml

配置文件的作用:修改SpringBoot自动配置的默认值;SpringBoot在底层都给我们自动配置好;

1.1 基本语法

k:(空格)v:表示一对键值对(空格必须有);
以空格的缩进来控制层级关系;只要是左对齐的一列数据,都是同一个层级的

server:
	port: 8081
	path: /hello

属性和值是大小写敏感的

1.2 值的写法

1.2.1 字面量:普通的值(数字,字符串,布尔)

  • k: v:字面直接来写;

字符串默认不用加上单引号或者双引号;

  • “”:双引号;不会转义字符串里面的特殊字符;特殊字符会作为本身想表示的意思
    name: “zhangsan \n lisi”:输出;zhangsan 换行 lisi
  • ‘’:单引号;会转义特殊字符,特殊字符最终只是一个普通的字符串数据
    name: ‘zhangsan \n lisi’:输出;zhangsan \n lisi

1.2.2 对象、Map(属性和值)(键值对):

  • k: v:在下一行来写对象的属性和值的关系;注意缩进
    对象还是k: v的方式
friends:
	lastName: zhangsan
	age: 20

行内写法:

friends: {lastName: zhangsan,age: 18}

1.2.3 数组(List、Set):

  • 用- 值表示数组中的一个元素
	pets:
		‐ cat
		‐ dog
		‐ pig
  • 行内写法
pets: [cat,dog,pig]	

1.3 配置文件注入

配置文件

person:
	lastName: hello
	age: 18
	boss: false
	birth: 2017/12/12
	maps: {k1: v1,k2: 12}
	lists:
		‐ lisi
		‐ zhaoliu
	dog:
		name: 小狗
		age: 12

javaBean

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:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定;
 * prefix = "person":配置文件中哪个下面的所有属性进行一一映射
 * 只有这个组件是容器中的组件,才能容器提供的@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;
    
    // 注意 必须加 get / set 方法
}

导入配置文件处理器

   
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-configuration-processorartifactId>
            <version>2.2.6.RELEASEversion>
        dependency>

补充 :properties配置文件在idea中默认utf-8可能会乱码

SpringBoot学习笔记(2) - YAML配置文件_第1张图片

1.3.1 @Value获取值和@ConfigurationProperties获取值比较

SpringBoot学习笔记(2) - YAML配置文件_第2张图片

  • 如果 只是在某个业务逻辑中需要获取一下配置文件中的某项值,使用@Value;
  • 如果 专门编写了一个javaBean来和配置文件进行映射,我们就直接使用@ConfigurationProperties;

1.3.2 配置文件注入值数据校验

需要添加相关依赖

  <dependency>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starter-webartifactId>
 dependency>

@Component
@ConfigurationProperties(prefix = "person")
// 参数校验相关注解
@Validated
public class Person {

    // 限制 lastName必须是邮箱格式, 如果不是邮箱格式会报错
    @Email
    private String lastName;
    private Integer age;
    private Boolean boss;
    private Date birth;
    private Map<String, Object> maps;
    private List<Object> lists;
    private Dog dog;
  • @Validated
  • 参数校验相关 :限制 lastName必须是邮箱格式, 如果不是邮箱格式会报错
    @Email
    如果参数格式不符合会报错 :
    SpringBoot学习笔记(2) - YAML配置文件_第3张图片

1.3.3 @ImportResource注解

作用 :


导入Spring的配置文件,让配置文件里面的内容生效;
Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别;
想让Spring的配置文件生效,加载进来;@ImportResource标注在一个配置类上


  1. 在resources目录下创建一个 beans.xml 文件

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="dog" class="com.wangt.springboot.chapter01.domain.Dog">bean>
beans>
  1. 在 Application类中添加注解:@ImportResource
/**
 * @author wangt
 */
@ImportResource(locations = {"classpath:beans.xml"})
@SpringBootApplication
public class Chapter01Application {

    public static void main(String[] args) {
        SpringApplication.run(Chapter01Application.class, args);
    }

}
  1. 测试Bean是否在容器内创建
@RunWith(SpringRunner.class)
@SpringBootTest
class Chapter01ApplicationTests {
    @Autowired
    ApplicationContext ioc;

    @Test
    void testDog(){
       boolean isContain = ioc.containsBean("dog");
        System.out.println(isContain);
    }
}

1.3.4 @PropertySource 加载指定的配置文件

  1. 创建指定的配置文件
# student
student.last-name=李白${random.uuid}
student.age=${random.int}
student.boss=false
student.maps.k1=KK
student.maps.v1=21
student.lists=a1, a2, a3

2.创建Person类,并指定

1.4 配置文件占位符

你可能感兴趣的:(SpringBoot学习笔记)