一、为什么需要加载局部配置文件
使用 @Value 或 @ConfigurationProperties 为属性注入配置文件中的值时,默认是去全局配置文件(application.properties 或 application.yml)去寻找对应的值。
但是如果都将配置放入该文件,不方便找到,所以可以使用局部配置文件。
在类上使用 @PropertySource 指定对应的局部配置文件,即可读入局部配置文件的配置。
@PropertySource(value = {"配置文件位置"})
可同时制定多个配置文件,在 {} 中使用逗号(,)隔开即可。
二、加载局部配置文件示范
1、新建局部配置文件
在 src/main/resources 下新建局部配置文件 demo.properties
# 配置 demo 的值
demo.last-name=lisi
demo.age=30
demo.birthday=1989/9/12
demo.boss=false
demo.salary=23000
demo.map.key1=value1
demo.map.key2=value2
demo.list=one, two, three
demo.data-demo.name=zhangsan
demo.data-demo.age=20
2、在类上加上 @PropertySource,并指定局部配置文件的位置
package com.example.springbootboot02config.bean;
import org.springframework.beans.factory.annotation.Value;
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 javax.validation.constraints.Email;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* @author liyanan
* @date 2019/12/16 13:43
*/
@PropertySource(value = {"classpath:demo.properties"}) // 加载局部配置文件
@ConfigurationProperties(prefix = "demo")
@Component
@Validated
public class Demo {
private String lastName;
private Integer age;
private Double salary;
private Boolean boss;
private Date birthday;
private Map<String, Object> map;
private List<String> list;
private DataDemo dataDemo;
private static String config;
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getAge() {
return age;
}
public void setConfig(String config) {
Demo.config = config;
}
public void setAge(Integer age) {
this.age = age;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public Boolean getBoss() {
return boss;
}
public void setBoss(Boolean boss) {
this.boss = boss;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Map<String, Object> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public DataDemo getDataDemo() {
return dataDemo;
}
public void setDataDemo(DataDemo dataDemo) {
this.dataDemo = dataDemo;
}
public static String getConfig() {
return config;
}
@Override
public String toString() {
return "Demo{" +
"lastName='" + lastName + '\'' +
", age=" + age +
", salary=" + salary +
", boss=" + boss +
", birthday=" + birthday +
", map=" + map +
", list=" + list +
", dataDemo=" + dataDemo +
'}';
}
}
3、测试是否注入成功
package com.example.springbootboot02config;
import com.example.springbootboot02config.bean.Demo;
import com.example.springbootboot02config.bean.Emp;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SpringbootBoot02ConfigApplicationTests {
@Autowired
Demo demo;
@Test
void contextLoads() {
System.out.println(demo);
}
}
运行结果为:
Demo{lastName='lisi', age=30, salary=23000.0, boss=false, birthday=Tue Sep 12 00:00:00 CDT 1989, map={key2=value2, key1=value1}, list=[one, two, three], dataDemo=DataDemo{name='zhangsan', age=20}}