啟動(dòng)加載數(shù)據(jù) CommandLineRunner

我們會(huì)有在項(xiàng)目服務(wù)啟動(dòng)的時(shí)候就去加載一些數(shù)據(jù)或做一些事情這樣的需求,為了解決這樣的問(wèn)題,在Spring Boot 中,實(shí)現(xiàn)接口 CommandLineRunner 即可。

創(chuàng)建實(shí)現(xiàn)接口 CommandLineRunner 的類:

/**
 * 監(jiān)聽(tīng)完成時(shí)觸發(fā)
 * 
 * @author : Fredia
 * @since : 2018年3月16日
 * @version : v1.0.0
 */
@Configuration
public class AuthClientRunner implements CommandLineRunner {

    @Autowired
    private ServiceAuthConfig serviceAuthConfig;
    @Autowired
    private UserAuthConfig userAuthConfig;
    @Autowired
    private ServiceAuthFeign serviceAuthFeign;

    @Override
    public void run(String... args) throws Exception {
        log.info("初始化加載用戶pubKey");
        try {
            refreshUserPubKey();
        }catch(Exception e){
            log.error("初始化加載用戶pubKey失敗,1分鐘后自動(dòng)重試!",e);
        }
        log.info("初始化加載客戶pubKey");
        try {
            refreshServicePubKey();
        }catch(Exception e){
            log.error("初始化加載客戶pubKey失敗,1分鐘后自動(dòng)重試!",e);
        }
    }
 \    public void refreshUserPubKey(){
        BaseResponse resp = serviceAuthFeign.getUserPublicKey(serviceAuthConfig.getClientId(), serviceAuthConfig.getClientSecret());
        if (resp.getStatus() == HttpStatus.OK.value()) {
            ObjectRestResponse<byte[]> userResponse = (ObjectRestResponse<byte[]>) resp;
            this.userAuthConfig.setPubKeyByte(userResponse.getData());
        }
    }
 
    public void refreshServicePubKey(){
        BaseResponse resp = serviceAuthFeign.getServicePublicKey(serviceAuthConfig.getClientId(), serviceAuthConfig.getClientSecret());
        if (resp.getStatus() == HttpStatus.OK.value()) {
            ObjectRestResponse<byte[]> userResponse = (ObjectRestResponse<byte[]>) resp;
            this.serviceAuthConfig.setPubKeyByte(userResponse.getData());
        }
    }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,569評(píng)論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,273評(píng)論 6 342
  • 實(shí)際應(yīng)用中,我們會(huì)有在項(xiàng)目服務(wù)啟動(dòng)的時(shí)候就去加載一些數(shù)據(jù)或做一些事情這樣的需求。為了解決這樣的問(wèn)題,spring ...
    程序員古德閱讀 1,943評(píng)論 0 1
  • 據(jù)說(shuō),人成形前會(huì)有三世。第一世,是那地獄底的苦工,戴著鐐鎖,不知疲倦的勞作,這時(shí)是沒(méi)有凡胎的只是一只氣的魂靈,皮囊...
    心疼兔子閱讀 788評(píng)論 0 2
  • Trie樹(shù),即字典樹(shù),又稱單詞查找樹(shù)。經(jīng)常應(yīng)用于字符串的統(tǒng)計(jì)與排序,經(jīng)常被搜索引擎系統(tǒng)用于文本詞頻統(tǒng)計(jì)。 核心思想...
    wayyyy閱讀 530評(píng)論 0 0

友情鏈接更多精彩內(nèi)容