springboot学习笔记(三)- yml的使用

一、yaml语法学习:

1、配置文件:

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

(1)application.properties 语法结构 :key=value
(2)application.yml 语法结构:key:空格 value

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

比如我们可以在配置文件中修改Tomcat 默认启动的端口号!测试一下!

server.port=8081

2、基础语法

说明:语法要求严格!
1、空格不能省略
2、以缩进来控制层级关系,只要是左边对齐的一列数据都是同一个层级的。
3、属性和值的大小写都是十分敏感的。
注意:
(1)“ ” 双引号,不会转义字符串里面的特殊字符 , 特殊字符会作为本身想表示的意思;
比如 :name: “kuang \n shen” 输出 :kuang 换行 shen
(2)’’ 单引号,会转义特殊字符 , 特殊字符最终会变成和普通字符一样输出
比如 :name: ‘kuang \n shen’ 输出 :kuang \n shen

3、类型介绍:
(1)字面量:普通的值 [ 数字,布尔值,字符串 ]

k: v

(2)对象、Map(键值对)

#对象、Map格式
k: 
    v1:
    v2:1)第一种写法:
student:
    name: qinjiang
    age: 32)第二种写法:
student: {name: qinjiang,age: 3}

(3)数组( List、set ):用 - 值表示数组中的一个元素,比如:

pets:
 - cat
 - dog
 - pig

pets: [cat,dog,pig]

二、注入配置文件实例

注意: 一定要有无参构造方法

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

public class Dog {
    String name;
    Integer age;
    Dog(){
        super();
    }

    public Dog(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getAge() {
        return age;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

@ConfigurationProperties(prefix = “person”)要加上

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    String name;
    Integer age;
    Dog dog;
    Date birth;
    private Map<String,Object> maps;
    private List<Object> lists;

    public Person(String name, Integer age, Dog dog, Date birth, Map<String, Object> maps, List<Object> lists) {
        this.name = name;
        this.age = age;
        this.dog = dog;
        this.birth = birth;
        this.maps = maps;
        this.lists = lists;
    }
    Person(){

    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", dog=" + dog +
                ", birth=" + birth +
                ", maps=" + maps +
                ", lists=" + lists +
                '}';
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    public void setMaps(Map<String, Object> maps) {
        this.maps = maps;
    }

    public void setLists(List<Object> lists) {
        this.lists = lists;
    }

    public String getName() {
        return name;
    }

    public Integer getAge() {
        return age;
    }

    public Dog getDog() {
        return dog;
    }

    public Date getBirth() {
        return birth;
    }

    public Map<String, Object> getMaps() {
        return maps;
    }

    public List<Object> getLists() {
        return lists;
    }
}

person:
  name: HuangEnfei
  age: 23
  birth: 2020/01/01
  maps:
    movie: today
    song: no.1
  lists:
    - today
    - is
    - sunday
  dog:
    name: xiaobai
    age: 4

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值