Spring Boot(4)配置篇 - @PropertySource 读取指定配置文件

我们在开发中经常会将某一些配置文件从全局配置文件中分离,写在单独的配置文件中,但是此时SpringBoot将无法从默认的配置文件中获取我们需要的配置映射。

使用 @PropertySource就可以帮我们完成指定配置文件下的配置映射

首先我们在resources目录下创建一个新的配置文件conf-person.properties

person.name=小明
person.age=6
person.boss=false
person.dirth=2025/01/01
person.maps.k1=v1
person.maps.k2=v2
person.lists={l1,l2,l3}
person.dog.name=小白
person.dog.age=2

在我们的配置文件类中添加@PropertySource注解

@Component
@PropertySource(value = {"classpath:conf-person.properties"})
@ConfigurationProperties(prefix = "person")
public class Person {

    private String name;
    private Integer age;
    private Boolean boss;
    private Date dirth;
    private Map maps;
    private List lists;
    private Dog dog;

可以发现PropertySource是可以支持多个properties配置文件的

你可能感兴趣的:(Spring,Boot)