在 Spring Boot 项目中resource
目录下创建一个simple.yml
文件
my:
enjoy:
website:
- github
- google
open_source: spring
like:
food: chicken
pc: Thinkpad,MacBook Pro
方式一:使用 YamlPropertiesFactoryBean
把 yaml 文件注入到系统配置中,示例如下:
import lombok.Data;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;
import java.util.List;
@Data
@Component
@ConfigurationProperties(prefix = "my.enjoy")
public class SimpleYaml implements InitializingBean {