yaml基础语法、给属性赋值、和properties的区别


yaml基础语法

说明:语法要求严格!

1、空格不能省略

2、以缩进来控制层级关系,只要是左边对齐的一列数据都是同一个层级的。

3、属性和值的大小写都是十分敏感的。

yaml给属性赋值

@ConfigurationProperties 作用:

将配置文件中配置的每一个属性的值,映射到这个组件中。告诉spingboot将本类中的所有属性和配置文件中相关的配置进行绑定。

参数prefix = "person" :将配置文件中的person下面的所有属性一一对应。

(只有这个组件是容器中的组件才能使用容器提供的@ConfigurationProperties 功能)

person.java

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	//注册bean
@ConfigurationProperties(prefix = "person")
public class Person {
    private String name;
    private Integer age;
    private Boolean happy;
    private Date birth;
    private Map<String, Object> maps;
    private List<Object> lists;

    public Person() {
    }

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

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

    public Boolean getHappy() {
        return happy;
    }

    public void setHappy(Boolean happy) {
        this.happy = happy;
    }

    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;
    }

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

application.yaml

person:
  name: wen
  age: 18
  happy: true
  birth: 2000/01/01
  maps: {k1: v1,k2: v2}
  lists:
    - code
    - sing
    - draw

Boot01ApplicationTests.java

@SpringBootTest
class Boot01ApplicationTests {
    @Autowired
    private Person person;

    @Test
    void contextLoads() {
        System.out.println(person);
    }

}

运行结果:

另外:配置文件除了yaml还有properties,properties配置文件在写中文的时候会有乱码,我们需要去IDEA中设置编码格式为UTF-8

yaml和properties的区别

总结:

  • 配置yml和配置properties都可以获取到值,强烈推荐yml
  • 如果我们在某个业务中,只需要获取配置文件中的某个值,可以使用一下@value
  • 如果我们专门编写了一个JavaBean来和配置文件进行映射,就直接使用@configurationProperties

松散绑定

(这里的firstName和first-name是一样的,-后面的字母默认是大写的)

结果:

JSR303校验

需要添加依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

yaml里的格式不是邮箱格式,所以会报错:


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM