springboot读取yaml文件

*本文章属于个人记录笔记,无其他用途

1.resources文件夹下增加ymal文件(此处文件名application_test.yml)

person:
  name: 张三
  sex: 男
  age: 12
  marry: false
  hobby:
    - 喝可乐
    - 打游戏
    - 睡大觉
  birth: 2009-03-04
  dog:
    name: 塞班
    age: 2

2.新增PropertySourceFactory接口的YMAL文件实现类(借鉴某大佬写好的实现类代码)

package com.example.demo.Impl;

import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
import java.io.IOException;
import java.util.Properties;

public class YAMLPropertySourceFactory implements PropertySourceFactory {

    @Override
    public PropertySource createPropertySource(String name, EncodedResource encodedResource) throws IOException {
        //创建一个YAML解析工厂。
        YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
        //设置资源。
        factory.setResources(encodedResource.getResource());

        //获取解析后的Properties对象
        Properties properties = factory.getObject();
        //返回。此时不能像默认工厂那样返回ResourcePropertySource对象 ,要返回他的父类PropertiesPropertySource对象。
        return name != null ? new PropertiesPropertySource(name, properties) :
                new PropertiesPropertySource(encodedResource.getResource().getFilename(),properties);
    }
}

3.增加两个对象Person和Dog

//Person代码段
package com.example.demo.target;

import com.example.demo.Impl.YAMLPropertySourceFactory;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import java.util.Date;
import java.util.List;

@Data   //代替手动输入get/set
@Component
@PropertySource(value = "classpath:application_test.yml", factory = YAMLPropertySourceFactory.class)    //设置yaml路径及使用解析的实现类文件
@ConfigurationProperties(prefix = "person") //绑定yaml
@Validated  //数据校验
public class Person {
    private String name;
    private String sex;
    private int age;
    private Boolean marry;
    private Date birth;
    private List hobby;
    private Dog dog;
}
//Dog代码段
package com.example.demo.target;

import lombok.Data;
import org.springframework.stereotype.Component;

@Component
@Data
public class Dog {
    private String name;
    private int age;
}

4.进行测试

package com.example.demo;

import com.example.demo.target.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class Spring_Boot_Test {
    @Autowired
    private Person person;
    @Test
    void contextLoads() {
        System.out.println("------开始测试------");
        System.out.println(person);
        System.out.println("------测试结束------");
    }
}

打印结果:

------开始测试------
Person(name=张三, sex=男, age=12, marry=false, birth=Wed Mar 04 08:00:00 CST 2009, hobby=[喝可乐, 打游戏, 睡大觉], dog=Dog(name=塞班, age=2))
------测试结束------

5.在其他类中调用该类时,需要使用@Autowired进行自动装配,从而获取改类的属性及方法

package com.example.demo.controller;

import com.example.demo.target.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("demo")
public class Demo01 {
    @Autowired    //进行自动装配
    Person person;
    @RequestMapping("demo01")
    public String demo1(){
        return person.toString();
    }
}

你可能感兴趣的:(JAVA,spring,boot,java,spring)