您的位置:首页 > 编程语言 > Java开发

java Properties工具

2015-06-05 17:06 429 查看
import java.util.Enumeration;

import java.util.ResourceBundle;

import java.util.concurrent.ConcurrentHashMap;

import java.util.concurrent.ConcurrentMap;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

public class PropertiesUtil {

    private static final Logger log = LoggerFactory.getLogger(PropertiesUtil.class);

    public static ConcurrentMap< String, String >    appExceptionMessageMap    = new ConcurrentHashMap< String, String >();

    public static ConcurrentMap<String,String> responseMessageMap = new ConcurrentHashMap<String, String>();

    public static ConcurrentMap<String,String> interfaceAuthorityMap = new ConcurrentHashMap<String, String>();

    public static ConcurrentMap<String,String> configMap = new ConcurrentHashMap<String, String>();

    static {

        init();

    }

    private static void init(){

        try{

            ResourceBundle exceptionMessage = ResourceBundle.getBundle("props/error-message");

            ResourceBundle responseMessage = ResourceBundle.getBundle("props/response-message");

            ResourceBundle interfaceAuthority = ResourceBundle.getBundle("props/interface-authority");

            ResourceBundle config = ResourceBundle.getBundle("props/config");

            initConfig(exceptionMessage,appExceptionMessageMap);

            initConfig(responseMessage,responseMessageMap);

            initConfig(interfaceAuthority,interfaceAuthorityMap);

            initConfig(config, configMap);

            

        } catch (Throwable t){

            log.error("cofig init error .............",t);

        }

    }

    private static void initConfig(ResourceBundle resourceBundle,ConcurrentMap map){

        Enumeration< String > e = resourceBundle.getKeys();

        while( e.hasMoreElements() ) {

            String key = e.nextElement();

            map.put( key, resourceBundle.getString( key ) != null ? resourceBundle.getString( key )

                    .trim() : null );

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: