apollo 從配置中獲取某個配置


 

 

 

 

 

package com.sea.comon.utils;

import java.text.MessageFormat;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigService;

/**
 * apollo國際化配置文件
 * 
 * @author Merlin
 *
 */
@Configuration
public class ApolloMessageUtil implements InitializingBean {

    private static Config enPublicConfig = ConfigService.getConfig("message"); // apollo 總創建的命名空間名字

    private static ApolloMessageUtil apolloMessage = null;

    public static ApolloMessageUtil getApolloMessage() {
        return apolloMessage;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        apolloMessage = this;
    }

    public static String getString(String key, Object[] params) {
        String msg = null;
        if (!StringUtils.isEmpty(key)) {

            String messageKey = enPublicConfig.getProperty(key, "");
            msg = MessageFormat.format(messageKey, params);
        }
        return msg;
    }
    
    public static String getString(String key) {
        String msg = null;
        if (!StringUtils.isEmpty(key)) {

            msg = enPublicConfig.getProperty(key, "");
        }
        return msg;
    }
    
    public static String getString(Config config ,String key) {
        String msg = null;
        if (!StringUtils.isEmpty(key)) {

            msg = config.getProperty(key, "");
        }
        return msg;
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM