springBoot 配置文件值 使用靜態方式賦值

記錄下方法:

package com.zbj.cs.chance.reformChanceBiz.util;

import com.zbj.cs.chance.reformChanceBiz.impl.NewChanceConfigBiz;

/**
 * @title 一句話說明功能
 * @author: wangtingsong
 * @Date: 2020/3/30 16:29
 * @since 版本號
 */
public class ControlNewChanceUtils {
    //新邏輯執行開關是否打開
    public static String NEW_CHANCE_ONOFF;
    //新邏輯日誌入庫開關是否打開
    public static String NEW_CHANCE_INSERT_LOG;
    //新邏輯推送消息開關是否打開
    public static String NEW_CHANCE_SEND_MESSAGE;

    public static void setNewChanceConfig(NewChanceConfigBiz newChanceConfig){
        ControlNewChanceUtils.NEW_CHANCE_ONOFF = newChanceConfig.getNewChanceOnOff();
        ControlNewChanceUtils.NEW_CHANCE_INSERT_LOG = newChanceConfig.getOnOffLog();
        ControlNewChanceUtils.NEW_CHANCE_SEND_MESSAGE = newChanceConfig.getOnOffMessage();
    }

    public static boolean checkOnOff(){
        return "yes".equals(ControlNewChanceUtils.NEW_CHANCE_ONOFF);
    }

    public static boolean checkOnOffLog(){
        return "yes".equals(ControlNewChanceUtils.NEW_CHANCE_INSERT_LOG);
    }

    public static boolean checkOnOffSendMessage(){
        return "yes".equals(ControlNewChanceUtils.NEW_CHANCE_SEND_MESSAGE);
    }
}

 

使用 @PostConstruct註解方式 實例化bean的時候 進行初始化讀取值

package com.zbj.cs.chance.reformChanceBiz.impl;

import com.zbj.cs.chance.reformChanceBiz.util.ControlNewChanceUtils;
import javax.annotation.PostConstruct;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

/**
 * @title 一句話說明功能
 * @author: wangtingsong
 * @Date: 2020/3/30 16:45
 * @since 版本號
 */
@Configuration
@Data
public class NewChanceConfigBiz {

    @Value("${new.chance.onOff}")
    private String newChanceOnOff;

    @Value("${new.chance.insert.log}")
    private String onOffLog;

    @Value("${new.chance.send.message}")
    private String onOffMessage;

    @PostConstruct
    public void init(){
        ControlNewChanceUtils.setNewChanceConfig(this);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章