springboot如何使用@ConfigurationProperties封裝配置文件
使用@ConfigurationProperties封裝配置文件
業務場景:
把配置文件的信息,讀取並自動封裝成實體類,可以使用@ConfigurationProperties,把同類的配置信息自動封裝成實體類。
1、在pom.xml中添加依賴包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
2、創建配置文件(application.properties)
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)
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)
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)
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,控制臺輸出結果
以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。
推薦閱讀:
- 關於springBoot yml文件的list讀取問題總結(親測)
- 如何設計一個安全的API接口詳解
- springboot整合騰訊雲短信開箱即用的示例代碼
- Springboot 如何指定獲取出 yml文件裡面的配置值
- Python調用騰訊雲短信服務發送手機短信