spring boot——配置文件——配置绑定—— @PropertySource 注解

@PropertySource 

 

如果将所有的配置都集中到 application.properties 或 application.yml 中,那么这个配置文件会十分的臃肿且难以维护,

 

因此我们通常会将与 Spring Boot 无关的配置(例如自定义配置)提取出来,写在一个单独的配置文件中,并在对应的 JavaBean 上使用 @PropertySource 注解指向该配置文件。

 

1. 以 helloworld 为例,将与 person 相关的自定义配置移动到 src/main/resources 下的 person.properties 中(注意,必须把 application.properties 或 application.yml 中的相关配置删除),如下图。

 

 

 

 

 

 

person.properties 的配置如下。

 

1
2
3
4
5
6
7
8
9
person.last-name=李四
person.age=12
person.birth=2000/12/15
person.boss=false
person.maps.k1=v1
person.maps.k2=14
person.lists=a,b,c
person.dog.name=dog
person.dog.age=2

  

 

 

 

 

 

 

2. 在 Person 使用 @PropertySource 注解指向 person.properties,代码如下。

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package net.biancheng.www.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 java.util.Date;
import java.util.List;
import java.util.Map;
 
@PropertySource(value = "classpath:person.properties")//指向对应的配置文件
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
 
    private String lastName;
 
    private Integer age;
 
    private Boolean boss;
 
    private Date birth;
 
    private Map<String, Object> maps;
 
    private List<Object> lists;
 
    private Dog dog;
 
    public Person() {
 
    }
 
    public String getLastName() {
        return lastName;
    }
 
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
 
    public Integer getAge() {
        return age;
    }
 
    public void setAge(Integer age) {
        this.age = age;
    }
 
    public Boolean getBoss() {
        return boss;
    }
 
    public void setBoss(Boolean boss) {
        this.boss = boss;
    }
 
    public Date getBirth() {
        return birth;
    }
 
    public void setBirth(Date birth) {
        this.birth = birth;
    }
 
    public Map<String, Object> getMaps() {
        return maps;
    }
 
    public void setMaps(Map<String, Object> maps) {
        this.maps = maps;
    }
 
    public List<Object> getLists() {
        return lists;
    }
 
    public void setLists(List<Object> lists) {
        this.lists = lists;
    }
 
    public Dog getDog() {
        return dog;
    }
 
    public void setDog(Dog dog) {
        this.dog = dog;
    }
 
    public Person(String lastName, Integer age, Boolean boss, Date birth, Map<String, Object> maps, List<Object> lists, Dog dog) {
        this.lastName = lastName;
        this.age = age;
        this.boss = boss;
        this.birth = birth;
        this.maps = maps;
        this.lists = lists;
        this.dog = dog;
    }
 
    @Override
    public String toString() {
        return "Person{" +
                "lastName='" + lastName + '\'' +
                ", age=" + age +
                ", boss=" + boss +
                ", birth=" + birth +
                ", maps=" + maps +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }
}

  

 

 

 

 

 

 

 

 

 

3. 重启项目,使用浏览器访问 “http://localhost:8081/hello”,结果如下图。

 

 

 
                                                                                          Spring Boot 将 person.properties 的属性注入

 

posted @   小白龙白龙马  阅读(102)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2021-06-05 测试开发进阶——Redis——Redis集合(Set)
2021-06-05 测试开发进阶——Redis——Redis字符串(String)
2021-06-05 测试开发进阶——Redis——Redis列表(list)
点击右上角即可分享
微信分享提示