Application.yml的自定義屬性的讀取方式
Application.yml的自定義屬性的讀取
以前我們會把一些常量,放在接口中,因為接口默認屬性就是靜態(tài)的,缺省了public static final
注意:既可以讀取自定義的屬性,也可以讀取官方封裝好的屬性。
以前,接口定義常量
public interface WeixinPayConstants { // 接口里面默認屬性就是靜態(tài)常量,可以缺深public static fianl
// APID
String APPID = "45645xxx2";
// 商戶號
String MCID = "48xxxxx787";
// 回調(diào)地址
String CALLBACK_URL = "https://wxxxxxlback";
// 私key
String APISECRET = "SDFLKxxxxx23423423";
}- 方式1:@Value
- 方式2:@ConfigurationProperties
spring中在application.yml中管理常量
方式1:@Value
第一步:在application.yml中自定義屬性
# 環(huán)境激活
spring:
profiles:
active: dev
#自定義屬性,把上面接口的常量放這,冒號后面至少要有一個空格
ksd:
weixin:
appid: 456453sdfsd52342
mcid: 48878787
callbackurl: https://www.kuangstudy.com/pay/callback
apiscret: SDFLKSDJFKLSJKLJ23423423第二步:使用@Value(“${}”)來注入使用
前提:注入的地方一定要已近交給了IOC容器管理
@Service
public class WeixinPayService {
@Value("${ksd.weixin.appid}")
private String appid;
@Value("${ksd.weixin.mcid}")
private String mcid;
@Value("${ksd.weixin.callbackurl}")
private String callbackurl;
@Value("${ksd.weixin.apisecret}")
private String apisecret;
public void testvalue(){
System.out.println(appid);
System.out.println(mcid);
System.out.println(callbackurl);
System.out.println(apisecret);
}
}方式2:@ConfigurationProperties
這種方式具有面向對象的特性,把屬性注入道屬性類中,而不是上面@Value注入道某一個屬性中。
第一步:@ConfigurationProperties(prefix =“路徑前綴”)定義一個配屬性類,且屬性一定要生成getter,setter方法
@ConfigurationProperties(prefix ="ksd.weixin")
public class WeixinPayProperties {
// appid
private String appid;
// 商戶號
private String mcid;
// 回調(diào)地址
private String callbackurl;
// api私鑰
private String apisecret;
public String getAppid() {
return appid;
}
public void setAppid(String appid) {
this.appid = appid;
}
public String getMcid() {
return mcid;
}
public void setMcid(String mcid) {
this.mcid = mcid;
}
public String getCallbackurl() {
return callbackurl;
}
public void setCallbackurl(String callbackurl) {
this.callbackurl = callbackurl;
}
public String getApisecret() {
return apisecret;
}
public void setApisecret(String apisecret) {
this.apisecret = apisecret;
}
}

第二步:在啟動類上或者屬性類上加上注解@EnableConfigurationProperties(WeixinPayProperties.class)
@Configuration
這兩個注解告訴springboot去加載這個屬性配置類以及去完成屬性注入
@ConfigurationProperties(prefix ="ksd.weixin")
@EnableConfigurationProperties(WeixinPayProperties.class)
@Configuration
public class WeixinPayProperties {
......
}第三步:注入.get方法使用
@Service
public class WeixinPayService {
@Autowired
private WeixinPayProperties weixinPayProperties;
public void testvalue2() {
System.out.println(weixinPayProperties.getAppid());
System.out.println(weixinPayProperties.getApisecret());
System.out.println(weixinPayProperties.getMcid());
System.out.println(weixinPayProperties.getCallbackurl());
}
}
創(chuàng)建屬性類的時候會有一個提示

這個警告是告訴,springboot確實可以去幫你完成屬性和配置文件中的屬性自動注入,但是不能在配置文件中自動提示
解決:
添加依賴
<!--把項目中的springboot自定義屬性配置類生成一個元素數(shù)據(jù)文件,這個文件可以生成以后
在未來的配置文件中,我們就達到和官方一致效果,可以自動提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
然后關掉配置文件,再重寫編譯
手動編譯:
mvn clean compile
用工具編譯

最終,實際開發(fā)中@Value用的較多
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Spring的@PreAuthorize注解自定義權限校驗詳解
這篇文章主要介紹了Spring的@PreAuthorize注解自定義權限校驗詳解,由于項目中,需要對外開放接口,要求做請求頭校驗,不做其他權限控制,所以準備對開放的接口全部放行,不做登錄校驗,需要的朋友可以參考下2023-11-11
詳解SpringBoot 解決攔截器注入Service為空問題
這篇文章主要介紹了詳解SpringBoot 解決攔截器注入Service為空問題的解決,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06
使用mybatisPlus的queryWrapper做左聯(lián)接,內(nèi)聯(lián)接方式
本文介紹了如何使用Mybatis-Plus的QueryWrapper進行SQL查詢,包括左連接、內(nèi)連接等操作,通過示例代碼展示了如何構建復雜的SQL查詢,并將結果存儲在List對象中返回,希望給讀者提供參考2025-03-03
SpringBoot?Validation提示信息國際化配置方式
這篇文章主要介紹了SpringBoot?Validation提示信息國際化配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02

