springboot如何使用@ConfigurationProperties封裝配置文件
更新時間:2021年08月11日 10:26:20 作者:知識追求者
springboot如何使用@ConfigurationProperties封裝配置文件的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
使用@ConfigurationProperties封裝配置文件
業(yè)務(wù)場景:
把配置文件的信息,讀取并自動封裝成實體類,可以使用@ConfigurationProperties,把同類的配置信息自動封裝成實體類。
1、在pom.xml中添加依賴包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
2、創(chuàng)建配置文件(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、創(chuàng)建測試類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、創(chuàng)建測試類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、創(chuàng)建測試類(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,控制臺輸出結(jié)果


以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Springboot自動配置與@Configuration配置類詳解
- SpringBoot中的配置類(@Configuration)
- SpringBoot2底層注解@Configuration配置類詳解
- SpringBoot配置@Configuration注解和@bean注解
- SpringBoot中@ConfigurationProperties實現(xiàn)配置自動綁定的方法
- SpringBoot中@ConfigurationProperties 配置綁定
- SpringBoot @ConfigurationProperties注解的簡單使用
- Springboot @Configuration @bean注解作用解析
- Springboot @Configuration與自動配置詳解
相關(guān)文章
SpringBoot高并發(fā)下控制限流的幾種實現(xiàn)方法
隨著業(yè)務(wù)的發(fā)展,高并發(fā)成為很多系統(tǒng)不得不面對的問題,限流作為一種常用的技術(shù)手段,可以幫助我們有效地控制請求的流量,避免系統(tǒng)因過載而崩潰,本文將介紹在Spring Boot應(yīng)用中實現(xiàn)限流的幾種方法,需要的朋友可以參考下2024-06-06
使用注解@Validated效驗VO參數(shù)是否合規(guī)
這篇文章主要為大家介紹了使用注解@Validated效驗VO參數(shù)是否合規(guī)過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05
spring多數(shù)據(jù)源配置實現(xiàn)方法實例分析
這篇文章主要介紹了spring多數(shù)據(jù)源配置實現(xiàn)方法,結(jié)合實例形式分析了spring多數(shù)據(jù)源配置相關(guān)操作技巧與使用注意事項,需要的朋友可以參考下2019-12-12
Spring事務(wù)事件監(jiān)控的實現(xiàn)
這篇文章主要介紹了Spring事務(wù)事件監(jiān)控的實現(xiàn)。本文首先會使用實例進行講解Spring事務(wù)事件是如何使用的,然后會講解這種使用方式的實現(xiàn)原理。感興趣的小伙伴們可以參考一下2018-10-10
spring boot中使用RabbitMQ routing路由詳解
本篇文章主要介紹了spring boot中使用RabbitMQ routing路由詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03

