前情提要:需要调用微信小程序官方生成小程序码的接口。
东拼西凑代码之后,终于把功能搞通了,然而发现取不到yml配置文件中写好的appid和secret值???
一顿查资料之后,改好了,下面记录一下。
QRCodeUtil类:
首先,类上加注解 @Component
其次,类中两个成员变量,注意在set方法上注解 @Value 来注入配置文件中的值
private static String API_KEY;
private static String SECRET;
@Value("${wx.miniapp.configs.appid}")
public void setAPI_KEY(String API_KEY) {
this.API_KEY = API_KEY;
}
@Value("${wx.miniapp.configs.secret}")
public void setSECRET(String SECRET) {
this.SECRET = SECRET;
}
application-dev.yml配置文件:
注意书写的缩进格式有要求
wx:
miniapp:
configs:
appid: wxxxxxxxxxxxxxxxxx
secret: nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
token: temp
aesKey: temp
msgDataFormat: JSON
说明:由于涉及隐私,这里appid和secret值用其他字母代替 :)
这样就取到配置文件中预先写好的数值了!