0%

配置文件值映射到类属性

文章字数:52,阅读全文大约需要1分钟

相比于@Value这个方法更加简便一点

person.properties

1
2
3
person:
lastName: hello
age: 18
1
2
3
4
5
6
7
8
9
10
11
12
13
// 1. 引入配置文件
@PropertySource(value = {"classpath:person.properties"})
@Component
// 2. 加载开头为person的
@ConfigurationProperties(prefix = "person")

public class Person {
// 3. 属性和配置文件的值对应
private String lastName;

private Integer age;

...