废话不多说直接上代码
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class SMSConfig {
private static Integer appId;
private static String appKey;
private static Integer templateId;
private static String smsSign;
@Value("${sms.appId}")
public void setAppId(Integer appId) {
this.appId = appId;
}
@Value("${sms.appKey}")
public void setAppKey(String appKey) {
this.appKey = appKey;
}
@Value("${sms.templateId}")
public void setTemplateId(Integer templateId) {
this.templateId = templateId;
}
@Value("${sms.smsSign}")
public void setSmsSign(String smsSign) {
this.smsSign = smsSign;
}
public static Integer getAppId() {
return appId;
}
public static String getAppKey() {
return appKey;
}
public static Integer getTemplateId() {
return templateId;
}
public static String getSmsSign() {
return smsSign;
}
}
yml文件:
sms:
appId: *******
appKey: *******
templateId: *******
smsSign: *******
注意点:
set方法不能static修饰
非静态的直接用@Value("${xx.xxx}")就行,要取属性值的对象必须注入到spring中,否则也取不到值