美文网首页
通过ResourceLoader实现读取自定义参数文件

通过ResourceLoader实现读取自定义参数文件

作者: 春苟哈皮 | 来源:发表于2018-11-07 18:14 被阅读0次

    通过properties文件保存一些基本不会发生变化的键值对是一种非常好的设计思路,提高代码简洁性。

    public class PropertiesLoader {
        private static Logger logger = LoggerFactory.getLogger(PropertiesLoader.class);
        private static ResourceLoader resourceLoader = new DefaultResourceLoader();
        private Properties properties;
    
        public PropertiesLoader(String... resourcesPaths) {
            properties = loadProperties(Arrays.asList(resourcesPaths));
        }
    
        /**
         * 载入多个文件, 文件路径使用Spring Resource格式.
         */
        private Properties loadProperties(List<String> resourcesPaths) {
            Properties props = new Properties();
    
            for (String location : resourcesPaths) {
    
                InputStream is = null;
                try {
                    Resource resource = resourceLoader.getResource(location);
                    is = resource.getInputStream();
                    props.load(is);
                }catch (FileNotFoundException e){
                    // not trans--
                } catch (Exception e) {
                    logger.info("Could not load properties from path:" + location + ", " + e.getMessage());
                } finally {
                    IOUtils.closeQuietly(is);
                }
            }
            return props;
        }
    
        /**
         * 取出String类型的Property,但以System的Property优先,如果都为Null则抛出异常.
         */
        public String getProperty(String key) {
            String value = getValue(key);
            if (value == null) {
                throw new NoSuchElementException();
            }
            return value;
        }
    
        /**
         * 取出Property,但以System的Property优先,取不到返回空字符串.
         */
        private String getValue(String key) {
            String systemProperty = System.getProperty(key);
            if (systemProperty != null) {
                return systemProperty;
            }
            if (properties.containsKey(key)) {
                return properties.getProperty(key);
            }
            return "";
        }
    
    }
    
    public class ConfigUtil {
    
        static PropertiesLoader GLOB_CONFIG = null;
    
        static {
            init();
        }
    
        private static void init() {
            //输入配置文件名,一个或多个
            GLOB_CONFIG = new PropertiesLoader("companyName.properties");
        }
    
        public static String getConfig(String key) {
            if (GLOB_CONFIG == null) {
                init();
            }
            String value = GLOB_CONFIG.getProperty(key);
            if (StringUtils.isEmpty(value)) {
                value = "";
            }
            return value;
        }
    }
    

    通过上面的代码实现后,我们可以通过
    ConfigUtil.getConfig("propertiesName")来获取到配置好的键对应的值。

    相关文章

      网友评论

          本文标题:通过ResourceLoader实现读取自定义参数文件

          本文链接:https://www.haomeiwen.com/subject/wrzvxqtx.html