zoukankan      html  css  js  c++  java
  • spring项目读取配置的demo

    背景

    读取配置是基础能力,研发这个模式不错,可以从不同配置中读取数据,如下图:

    可以根据不同分类的文件来管理配置,然后统一在conf中配置哪些文件

    package com.jwen.platform.config;
    
    import com.jwen.platform.exception.AppConfigurationException;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.URISyntaxException;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.regex.Pattern;
    
    public enum Configurations {
        INSTANCE;
    
        private final static String KEY_VALUE_SEPARATOR = "=";
        private final static String CONFIGURATION_FILE = "application.conf";
        private final static String OTHER_CONFIGURATION_FILE_KEY = "app_include_file";
    
        private List<String> otherConfigurationFiles = new ArrayList<>();
        private Map<String, String> configurations = new HashMap<>();
    
        Configurations() {
            try {
                init();
            } catch (IOException | URISyntaxException e) {
                throw new AppConfigurationException("has error in your application.conf", e);
            }
        }
    
        private void init() throws IOException, URISyntaxException {
    
            InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(CONFIGURATION_FILE);
            BufferedReader br = new BufferedReader(new InputStreamReader(resourceAsStream));
    
            String s = "";
            List<String> lines = new ArrayList<String>();
    
            while ((s = br.readLine()) != null) {
                lines.add(s);
            }
    
            // 关闭流
            resourceAsStream.close();
            br.close();
    
            initConfigurations(lines);
            initOtherConfigurations();
    
        }
    
        private void initOtherConfigurations() throws URISyntaxException, IOException {
            for (String fileName : otherConfigurationFiles) {
                InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(fileName);
                BufferedReader br = new BufferedReader(new InputStreamReader(resourceAsStream));
    
    
                String s = "";
                List<String> lines = new ArrayList<String>();
    
                while ((s = br.readLine()) != null) {
                    lines.add(s);
                }
    
                // 关闭流
                resourceAsStream.close();
                br.close();
    
    
                initConfigurations(lines);
            }
    
            otherConfigurationFiles = null;
        }
    
        private boolean isOtherConfigurationKey(String key) {
            return OTHER_CONFIGURATION_FILE_KEY.equalsIgnoreCase(key);
        }
    
        private void initConfigurations(List<String> lines) throws URISyntaxException, IOException {
            lines.stream().forEach(line -> {
                int keyValueSeparatorIndex = line.indexOf(KEY_VALUE_SEPARATOR);
                if (isLegalConfigurationLine(line)) {
                    String key = line.substring(0, keyValueSeparatorIndex).trim();
                    String value = line.substring(keyValueSeparatorIndex + 1, line.length()).trim();
                    if (isOtherConfigurationKey(key)) {
                        otherConfigurationFiles.add(value);
                    } else {
                        configurations.put(key, value);
                    }
                }
            });
        }
    
        private boolean isLegalConfigurationLine(String line) {
            Pattern pattern = Pattern.compile("^[a-zA-Z](.*?)=(.*?)");
            return pattern.matcher(line).matches();
        }
    
    
        public String get(String key) {
            return configurations.get(key);
        }
    
        public static void main(String[] args) {
    
            System.out.println(Configurations.INSTANCE.get("test.on"));
        }
    }
    
  • 相关阅读:
    触摸屏调试【转】
    Android电容屏(二):驱动调试分析【转】
    linux输入子系统简述【转】
    Linux与Android 多点触摸协议【转】
    中断下半部tasklet【转】
    【转】轻量级文本编辑器,Notepad最佳替代品:Notepad++--不错
    【转】Android开发中adb启动失败adb连接异常的解决办法 offline
    【转】Java删除文件夹和文件
    【转】图说Android的8年演变史
    【转】基于 Android NDK 的学习之旅-----数据传输(引用数据类型)
  • 原文地址:https://www.cnblogs.com/jwentest/p/10223928.html
Copyright © 2011-2022 走看看