智慧屏 安装 app如何提升家庭娱乐与教育体验的关键工具
719
2022-12-20
springboot如何使用@ConfigurationProperties封装配置文件
使用@ConfigurationProperties封装配置文件
业务场景:
把配置文件的信息,读取并自动封装成实体类,可以使用@CuuvFENgWonfigurationProperties,把同类的配置信息自动封装成实体类。
1、在pom.xml中添加依赖包
2、创建配置文件(application.properties)
wx.appid = wx111111
wx.redirectUri = https://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 tehttp://mplateId;
}
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;
}
publhttp://ic 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.RestControlhttp://ler;
import com.jeff.config.SmsSettings;
import com.jeff.config.WxSettings;
@RestController
public class MyController {
@Autowired
private WxSettings wx;
@Autowired
private SmsSettings sms;
@Rehttp://questMapping("myTest")
public String myTest() {
System.out.println(wx.toString());
System.out.println(sms.toString());
return "success";
}
}
6、打开浏览器访问 http://localhost:8080/myTest,控制台输出结果
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~