springboot如何使用@ConfigurationProperties封裝配置文件
業(yè)務(wù)場(chǎng)景:
把配置文件的信息,讀取并自動(dòng)封裝成實(shí)體類(lèi),可以使用@ConfigurationProperties,把同類(lèi)的配置信息自動(dòng)封裝成實(shí)體類(lèi)。
1、在pom.xml中添加依賴(lài)包<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency>2、創(chuàng)建配置文件(application.properties)
wx.appid = wx111111wx.redirectUri = https://www.baidu.com/wx.templateId = 1wx.first = 模板標(biāo)題wx.remark = 模板備注wx.color = #000000sms.appid = 111111sms.appkey = bd3bfba026f711eaac3b005056b84de4sms.templateId = 1sms.sign = Jeff3、創(chuàng)建測(cè)試類(lèi)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;}@Overridepublic String toString() {return 'WxSettings [appid=' + appid + ', redirectUri=' + redirectUri + ', templateId=' + templateId + ', first='+ first + ', remark=' + remark + ', color=' + color + ']';}}4、創(chuàng)建測(cè)試類(lèi)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;}@Overridepublic String toString() {return 'SmsSettings [appid=' + appid + ', appkey=' + appkey + ', templateId=' + templateId + ', sign=' + sign+ ']';}}5、創(chuàng)建測(cè)試類(lèi)(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;@RestControllerpublic class MyController {@Autowiredprivate WxSettings wx;@Autowiredprivate SmsSettings sms;@RequestMapping('myTest')public String myTest() {System.out.println(wx.toString());System.out.println(sms.toString());return 'success';}}6、打開(kāi)瀏覽器訪問(wèn) http://localhost:8080/myTest,控制臺(tái)輸出結(jié)果
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Python如何批量生成和調(diào)用變量2. ASP.Net Core對(duì)USB攝像頭進(jìn)行截圖3. ASP.NET MVC實(shí)現(xiàn)橫向展示購(gòu)物車(chē)4. Python 中如何使用 virtualenv 管理虛擬環(huán)境5. Python獲取B站粉絲數(shù)的示例代碼6. Python基于requests實(shí)現(xiàn)模擬上傳文件7. python利用opencv實(shí)現(xiàn)顏色檢測(cè)8. windows服務(wù)器使用IIS時(shí)thinkphp搜索中文無(wú)效問(wèn)題9. ASP.Net Core(C#)創(chuàng)建Web站點(diǎn)的實(shí)現(xiàn)10. 通過(guò)CSS數(shù)學(xué)函數(shù)實(shí)現(xiàn)動(dòng)畫(huà)特效
