服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - springboot如何使用@ConfigurationProperties封装配置文件

springboot如何使用@ConfigurationProperties封装配置文件

2021-11-14 12:12知识追求者 Java教程

springboot如何使用@ConfigurationProperties封装配置文件的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

使用@ConfigurationProperties封装配置文件

业务场景:

把配置文件的信息,读取并自动封装成实体类,可以使用@ConfigurationProperties,把同类的配置信息自动封装成实体类。

1、在pom.xml中添加依赖包

?
1
2
3
4
5
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

2、创建配置文件(application.properties)

?
1
2
3
4
5
6
7
8
9
10
wx.appid = wx111111
wx.redirectUri = https://www.baidu.com/
wx.templateId = 1
wx.first = 模板标题
wx.remark = 模板备注
wx.color = #000000
sms.appid = 111111
sms.appkey = bd3bfba026f711eaac3b005056b84de4
sms.templateId = 1
sms.sign = Jeff

3、创建测试类1(WxSettings.java)

?
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
package com.jeff.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "wx")
public class WxSettings {
    private String appid;
    private String redirectUri;
    private Integer templateId;
    private String first;
    private String remark;
    private String color;
    public String getAppid() {
        return appid;
    }
    public void setAppid(String appid) {
        this.appid = appid;
    }
    public String getRedirectUri() {
        return redirectUri;
    }
    public void setRedirectUri(String redirectUri) {
        this.redirectUri = redirectUri;
    }
    public Integer getTemplateId() {
        return templateId;
    }
    public void setTemplateId(Integer templateId) {
        this.templateId = templateId;
    }
    public String getFirst() {
        return first;
    }
    public void setFirst(String first) {
        this.first = first;
    }
    public String getRemark() {
        return remark;
    }
    public void setRemark(String remark) {
        this.remark = remark;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    @Override
    public String toString() {
        return "WxSettings [appid=" + appid + ", redirectUri=" + redirectUri + ", templateId=" + templateId + ", first="
                + first + ", remark=" + remark + ", color=" + color + "]";
    }
}

4、创建测试类2(SmsSettings.java)

?
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
package com.jeff.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "sms")
public class SmsSettings {
    private String appid;
    private String appkey;
    private Integer templateId;
    private String sign;
    public String getAppid() {
        return appid;
    }
    public void setAppid(String appid) {
        this.appid = appid;
    }
    public String getAppkey() {
        return appkey;
    }
    public void setAppkey(String appkey) {
        this.appkey = appkey;
    }
    public Integer getTemplateId() {
        return templateId;
    }
    public void setTemplateId(Integer templateId) {
        this.templateId = templateId;
    }
    public String getSign() {
        return sign;
    }
    public void setSign(String sign) {
        this.sign = sign;
    }
    @Override
    public String toString() {
        return "SmsSettings [appid=" + appid + ", appkey=" + appkey + ", templateId=" + templateId + ", sign=" + sign
                + "]";
    }
}

5、创建测试类(MyController.java)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.jeff.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jeff.config.SmsSettings;
import com.jeff.config.WxSettings;
@RestController
public class MyController {
    @Autowired
    private WxSettings wx;
    @Autowired
    private SmsSettings sms;
    @RequestMapping("myTest")
    public String myTest() {
        System.out.println(wx.toString());
        System.out.println(sms.toString());
        return "success";
    }
}

6、打开浏览器访问 http://localhost:8080/myTest,控制台输出结果

springboot如何使用@ConfigurationProperties封装配置文件

springboot如何使用@ConfigurationProperties封装配置文件

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/weixin_45739720/article/details/103711280

延伸 · 阅读

精彩推荐