SpringBoot配置文件application.yml的理解

一、存放位置分类

1.当前项目根目录下的config目录下

2.当前项目的根目录下

3.resources目录下的config目录下

4.resources目录下

按照这上面的顺序,4个配置文件的优先级依次降低。

SpringBoot配置文件application.yml的理解_第1张图片

 二、自定义存放位置和自定义命名

自定义存放位置和自定义配置文件命令和application.properties配置类型,请参考一下SpringBoot配置文件application.properties的理解_qinxun2008081的博客-CSDN博客

三、yml属性特殊注入 

yml注册数组注入,例如

company:
  urls:
    - https://www.aa.com
    - https://www.bb.com

这些数据可以绑定到一个Bean类中

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

import java.util.ArrayList;
import java.util.List;

/**
 * @author qinxun
 * @date 2023-06-15
 * @Descripion: 测试
 */
@Component
@ConfigurationProperties(prefix = "company")
public class Website {

    private List urls = new ArrayList<>();

    public List getUrls() {
        return this.urls;
    }
}

测试

import com.example.springbootdemo.bean.Website;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringBootDemoApplicationTests {

    @Autowired
    private Website website;

    @Test
    void contextLoads() {
        // 输出 [https://www.aa.com, https://www.bb.com]
        System.out.println(website.getUrls());
    }

}

四、和application.properties的区别

1.properties文件是无序的,yml文件是有序的。

2.yml配置不支持@PropertySource注解

你可能感兴趣的:(SpringBoot,spring,boot,java,数据库)