springboot如何使用@ConfigurationProperties封裝配置文件
使用@ConfigurationProperties封裝配置文件
業(yè)務(wù)場(chǎng)景:
把配置文件的信息,讀取并自動(dòng)封裝成實(shí)體類,可以使用@ConfigurationProperties,把同類的配置信息自動(dòng)封裝成實(shí)體類。
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 = 模板標(biāo)題 wx.remark = 模板備注 wx.color = #000000 sms.appid = 111111 sms.appkey = bd3bfba026f711eaac3b005056b84de4 sms.templateId = 1 sms.sign = Jeff
3、創(chuàng)建測(cè)試類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)建測(cè)試類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)建測(cè)試類(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,控制臺(tái)輸出結(jié)果


以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Springboot自動(dòng)配置與@Configuration配置類詳解
- SpringBoot中的配置類(@Configuration)
- SpringBoot2底層注解@Configuration配置類詳解
- SpringBoot配置@Configuration注解和@bean注解
- SpringBoot中@ConfigurationProperties實(shí)現(xiàn)配置自動(dòng)綁定的方法
- SpringBoot中@ConfigurationProperties 配置綁定
- SpringBoot @ConfigurationProperties注解的簡(jiǎn)單使用
- Springboot @Configuration @bean注解作用解析
- Springboot @Configuration與自動(dòng)配置詳解
相關(guān)文章
springboot配置多數(shù)據(jù)源后mybatis攔截器失效的解決
這篇文章主要介紹了springboot配置多數(shù)據(jù)源后mybatis攔截器失效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
關(guān)于spring項(xiàng)目中無法加載resources下文件問題及解決方法
在學(xué)習(xí)Spring過程中,TestContext框架試圖檢測(cè)一個(gè)默認(rèn)的XML資源位置,再resources下創(chuàng)建了一個(gè)com.example的文件夾,執(zhí)行時(shí),報(bào)錯(cuò),本文給大家介紹spring項(xiàng)目中無法加載resources下文件,感興趣的朋友跟隨小編一起看看吧2023-10-10
MybatisPlus使用排序查詢時(shí)將null值放到最后
按照更新時(shí)間排序,但是更新時(shí)間可能為null,因此將null的數(shù)據(jù)放到最后,本文主要介紹了MybatisPlus使用排序查詢時(shí)將null值放到最后,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08
Java 把json對(duì)象轉(zhuǎn)成map鍵值對(duì)的方法
這篇文章主要介紹了java 把json對(duì)象中轉(zhuǎn)成map鍵值對(duì)的方法,本文的目的是把json串轉(zhuǎn)成map鍵值對(duì)存儲(chǔ),而且只存儲(chǔ)葉節(jié)點(diǎn)的數(shù)據(jù) 。需要的朋友可以參考下2018-04-04
Java實(shí)現(xiàn)圖片上傳至服務(wù)器功能(FTP協(xié)議)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)圖片上傳至服務(wù)器功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
spring-@Autowired注入與構(gòu)造函數(shù)注入使用方式
這篇文章主要介紹了spring-@Autowired注入與構(gòu)造函數(shù)注入使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12

