SpringBoot配置文件application.yml的理解

一、存放位置分类

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

2.当前项目的根目录下

3.resources目录下的config目录下

4.resources目录下

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

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

自定义存放位置和自定义配置文件命令和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<String> urls = new ArrayList<>();

    public List<String> 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注解

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qinxun2008081

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值