SpringBoot 配置文件值注入
文章目录
1.1 从全局配置文件中读取配置到实体类 (@ConfigurationProperties)
配置文件 (application.yml)(写在application.properties也可以,格式变一下即可)
person:
lastName: Mary
age: 16
birthDate: 2004/01/01
maps: {one:1,two:2,three:3}
lists:
- 1
- 2
- 3
pet:
name: wangcai
age: 3
实体类(javaBean):
/**
* 将配置文件中配置的每一个属性的值,映射到这个组件中
*
* @Component:
* 只有这个组件是容器中的组件,才能容器提供的@ConfigurationProperties功能;
*
* @ConfigurationProperties:
* 告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定(默认配置文件为application.properties与
* application.yml);
*
* prefix = "person":
* 配置文件中哪个下面的所有属性进行一一映射
*/
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String lastName;
private Integer age;
private Date birthDate;
private Map<String,Object> maps;
private List<Object> lists;
private Pet pet;
//省略get,set方法
@Override
public String toString() {
String mapValue = "";
if(this.maps!=null){
for(String key:this.maps.keySet()){
Object value = this.maps.get(key);
mapValue += key+"\t"+value+"\t";
}
}
return "lastName:"+this.lastName+
"\nage:"+this.age+
"\nbirthDate:"+this.birthDate+
"\nmaps:"+mapValue+
"\nlists:"+this.lists+
this.pet;
}
}
public class Pet {
private String name;
private Integer age;
//省略get,set方法
@Override
public String toString() {
return "\nPet:\n\tPet name:"+this.name+
"\n\tPet age:"+this.age+"\n";
}
}
我们可以导入配置文件处理器,以后编写配置就有提示了(pom.xml)
<!--导入配置文件处理器,配置文件进行绑定就会有提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
测试
@SpringBootTest
class DemoApplicationTests {
@Autowired
Person person;
@Test
void contextLoads() {
System.out.println(person.toString());
}
}
1.2 全局配置文件注入值(@Value)
@Value 其实是Spring中的注解,其功能使用xml文件描述是这样的:
<bean class="Person">
<property name="lastName" value="字面量/${key}从环境变量、配置文件中获取值/#{SpEL}"></property>
<bean/>
其中property表示的是Person类中的各项属性,name用于指定具体属性,value用于指定值。
value的值主要有3种:
- 字面量,也就是数字(1,2…)、字符串(abc)、布尔变量(true、false)等常量
- ${key},从环境变量、配置文件中获取值
- #{SpEL}, Spring的表达式语言,可以执行一些计算,调用一些函数
@Component
public class Person {
@Value("${person.last-name}") //@Value("#{'Lily'.toUpperCase()}")
private String lastName;
@Value("#{11*2}")
private Integer age;
private Date birthDate;
private Map<String,Object> maps;
private List<Object> lists;
private Pet pet;
//省略get,set和toString
}
1.3 从指定文件读取并配置实体类(@PropertySource+@ConfigurationProperties)
在 resources 目录下创建 person.properties
person.lastName = Lily
person.age = 20
person.birthDate = 2000/01/01
person.maps.one = 1
person.maps.two = 2
person.lists = a,b,ch
person.pet.name = wangcai
person.pet.age = 3
更改 Person 类中的注解
@Component
@PropertySource(value = {"classpath:person.properties"})
@ConfigurationProperties(prefix = "person")
public class Person {
//...省略属性,get,set,toString
}
要注意的是,PropertySource 只支持properties文件,不支持yml文件读取。
1.4 从指定文件读取并注入值 (@PropertySource+@Value)
创建配置文件
更改注释
@Component
@PropertySource(value = {"classpath:person.properties"})
public class Person {
@Value("${person.last-name}")
private String lastName;
//...省略属性,get,set,toString
}