1,什么是yml

一种新的配置方式,比XML更简单,比properties更强大,类似于json。

2,properties进行配置存在的问题

1,properties表达过于复杂,无法表达数据的内在联系

2,properties无法表达对象 集合类型【导致spring不能注解注入集合类型】

原有内容:

jdbc.driverName = com.mysql.cj.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/demo?serverTimezone=UTC
jdbc.username = root
jdbc.password = 1234
mybaatis.typeAliases = entity
@Component
@PropertySource("classpath:init.properties")
public class MapperProperties {
    @Value("${jdbc.driverName}")
    private String driverName;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;
    @Value("${mybaatis.typeAliases}")
    private  String typeAliases;
}

使用properties表达复杂,都有前缀,jdbc。无法表达数据对象的含义。

3,常用语法简介:

1,定义yml文件

后缀(yml,yaml)

2,语法:

1,基本语法:name: tomas  冒号之后有空格

2,对象概念:

account:

id:1【注意控制缩进】

password:1234

3,定义集合

list

   -1111

    -2222

spring整合yml

spring默认不支持yml。

spring #{value}的注入。properties的文件 【转换成property类形式】,类PropertySourcePlaceholderConfigure,对properties进行注入。

把yml转换成properties文件,然后让该类读取,然后属性注入。

YmlPropertiesFactoryBean这个类读取yml文件,然后转换成properties。见名知意,实现类FactoryBean接口。

1,准备yml配置文件

2,读取yml,转换成properties

3,应用PropertySourcePlaceholderConfigure类设置property。

4,类中@Value注入

 

处理集合问题时,用el表达式。比如 list:111,222

@Value(“#{‘${list}’}.split(‘,’)”)

对象类型的yaml进行配置时,过于繁琐。

springboot  引入@ConfigurationProperties().

springboot中yml的使用

参数类

package boot;


import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConstructorBinding;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
//使用配置属性注解,根据前缀自动进行注入
@ConfigurationProperties("datacate")
@Component
public class DataCate {

    private String name;
    private String[] habbits;
    private List<String>friends;
    private Map<String,Integer>maps;

    public DataCate() {
    }

    public DataCate(Map<String, Integer> maps) {
        this.maps = maps;
    }

    public DataCate(String name, String[] habbits, List<String> friends, Map<String, Integer> maps) {
        this.name = name;
        this.habbits = habbits;
        this.friends = friends;
        this.maps = maps;
        System.out.println("constructor");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        System.out.println("setter...");
        this.name = name;
    }

    public String[] getHabbits() {
        return habbits;
    }

    public void setHabbits(String[] habbits) {
        this.habbits = habbits;
    }

    public List<String> getFriends() {
        return friends;
    }

    public void setFriends(List<String> friends) {
        this.friends = friends;
    }

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

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

    @Override
    public String toString() {
        return "DataCate{" +
                "name='" + name + '\'' +
                ", habbits=" + Arrays.toString(habbits) +
                ", friends=" + friends +
                ", maps=" + maps +
                '}';
    }
}

yml文件

#指定使用的yml环境
spring:
  profiles:
    active: pro
---
#第一套环境
datacate:
  name: tomas
  habbits: [运动,打游戏,学习]
  friends:
    -小张
    -小王
    -小明
  maps:
    one: 1
    two: 2
    three: 3
    four: 4
#    为第一套环境指定名称
spring:
  config:
    activate:
      on-profile: dev
---
datacate:
  name: jackson
#  为第二套环境指定名称
spring:
  config:
    activate:
      on-profile: pro

注明:

yaml中‘—’表示分yaml配置文件。相当于多个yaml同时被导入同一个yaml中【毕竟application.yaml只有一个】。

本文地址:https://blog.csdn.net/weixin_46083389/article/details/113730613