SpringBoot项目优雅的实现多配置文件切换以及获取配置信息

SpringBoot项目优雅的实现多配置文件切换以及获取配置信息

在我们平时的生产中肯定不会单纯的的只有一个配置文件,通产会分为测试、开发、生产三个版本,做为一个刚入行的小白,记录一个实践中的技巧

一、构建项目

创建一个SpringBoot项目并在resources目录下创建像application-xxx.yml这样的配置文件。
在这里插入图片描述
进行配置文件切换

spring:
  application:
    name: test-server
  profiles:
    active: test/dev/prod ---进行切换配置文件版本
二、创建工具类进行解析yml获取对应的配置数据
  1. 创建工具类对yml进行简单的解析
public class YmlTools {
    private HashMap<String, String> ps = new LinkedHashMap<>();

    public YmlTools() {

    }

    public YmlTools(Resource resource) {
        load(resource);
    }

    public YmlTools(String path) {
        load(path);
    }

    public YmlTools load(String path) {
        ps.putAll(new YmlParser().load(path).getAll());
        return this;
    }

    public YmlTools load(Resource resource) {
        ps.putAll(new YmlParser().load(resource).getAll());
        return this;
    }

    public String getProperty(String key) {
        return ps.get(key);
    }

    public YmlTools clear() {
        ps.clear();
        return this;
    }

    public HashMap<String, String> getAll() {
        return ps;
    }

    public String dump() {
        StringBuilder sb = new StringBuilder();
        for (String key : ps.keySet()) {
            sb.append(key + ":" + ps.get(key) + "\n");
        }
        return sb.toString();
    }


    public static class YmlParser {
        private HashMap<String, String> ps;
        private Pattern pattern = Pattern.compile("\\$\\{[0-9a-zA-Z_\\.]+\\}");


        public String getProperty(String key) {
            return ps.get(key);
        }

        public HashMap<String, String> getAll() {
            return ps;
        }

        public YmlParser load(Resource resource) {
            ps = parseYml(resource);
            ps = parsePos();
            return this;
        }


        public YmlParser load(String path) {
            String classPathPreFix = "classpath:";
            if (path.startsWith(classPathPreFix)) {
                load(new ClassPathResource(path.substring(classPathPreFix.length())));
            } else {
                load(new FileSystemResource(path));
            }
            return this;
        }

        private String joinPrefix(String old, String append) {
            if (old == null || old.length() <= 0) {
                return append;
            }
            String rs = old + "." + append;
            if (rs.endsWith(".") || rs.endsWith(":"))
                rs = rs.substring(0, rs.length() - 1);
            return rs;
        }

        private HashMap<String, String> parseYml(Resource resource) {
            HashMap<String, String> ps = new LinkedHashMap<String, String>();
            BufferedReader br = null;
            try {
                InputStream is = resource.getInputStream();
                String line = null;
                br = new BufferedReader(new InputStreamReader(is));
                String currPrefix = "";
                int lastLevel = 0;
                while ((line = br.readLine()) != null) {
                    if (line.trim().length() <= 0 || line.trim().startsWith("#"))
                        continue;
                    int leftSpace = lineLeftSpace(line);

                    boolean hasVal = line.trim().contains(": ");
                    String k = null;
                    String v = null;
                    if (hasVal) {
                        String[] kv = line.trim().split(": ");
                        k = kv[0];
                        v = kv[1];
                    } else {
                        k = line.trim().split(":")[0];
                    }

                    currPrefix = changeToLevel(currPrefix, leftSpace / 2);
                    currPrefix = joinPrefix(currPrefix, k);

                    if (hasVal && null != v) {
                        ps.put(currPrefix, v);
                    }

                    lastLevel = leftSpace;


                }
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                if (br != null)
                    try {
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
            }

            return ps;
        }

        private String changeToLevel(String currPrefix, int level) {
            if (null == currPrefix || currPrefix.length() <= 0 || level == 0) {
                return "";
            }
            String[] grade = currPrefix.split("\\.");
            if (level >= grade.length) {
                return currPrefix;
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < level; i++) {
                if (sb.length() > 0)
                    sb.append(".");
                sb.append(grade[i]);
            }
            return sb.toString();
        }

        private String upPrefix(String currPrefix) {
            if (null != currPrefix) {
                if (currPrefix.contains("."))
                    currPrefix = currPrefix.substring(0, currPrefix.lastIndexOf("."));
                else
                    currPrefix = "";
            }

            return currPrefix;
        }

        private int lineLeftSpace(String line) {
            int l = 0;
            for (int i = 0; i < line.length(); i++) {
                if (line.charAt(i) == ' ') {
                    l++;
                } else if (line.charAt(i) == '\t') {
                    l += 2;
                } else {
                    break;
                }
            }
            return l;
        }

        private HashMap<String, String> parsePos() {
            HashMap<String, String> rs = new LinkedHashMap<>();
            for (String key : ps.keySet()) {
                rs.put(key, getPropertyWithParse(key));
            }
            return rs;
        }

        private String getPropertyWithParse(String key) {
            return replace(ps.get(key), new OnFind() {
                @Override
                public String replace(String source, String group, int start, int end) {
                    return getPropertyWithParse(group.substring(2, group.length() - 1));
                }
            });
        }

        private String replace(String source, OnFind onFind) {
            if(source==null){
                return source;
            }
            Matcher matcher = pattern.matcher(source);
            StringBuilder sb = new StringBuilder();

            int ls = 0;
            while (matcher.find()) {
                String group = matcher.group();
                int s = matcher.start();
                int e = matcher.end();
                sb.append(source.substring(ls, s));
                sb.append(onFind.replace(source, group, s, e));
                ls = e;
            }
            if (ls < source.length())
                sb.append(source.substring(ls));
            return sb.toString();
        }

        public interface OnFind {
            String replace(String source, String group, int start, int end);
        }
    }
}
  1. 创建一个获取配置文件的接口
/**
 * 配置文件调用接口
 */
@Service
public interface ProjectConfigService {

    /**
     * 配置文件值取得
     *
     * @param key 配置文件键
     * @return 配置文件值
     */
    String getProperty(String key);
}
  1. 实现该接口
@Service
public class ProjectConfigServiceImpl implements ProjectConfigService {

    //存储配置属性的Map集合
    private Map<String, String> conf = new HashMap<String, String>();
    public ProjectConfigServiceImpl(){
        conf.clear();
        conf.putAll(new YmlTools.YmlParser().load("classpath:application.yml").getAll());
        // 获取-后面的值
        String active = conf.get("spring.profiles.active");
        conf.putAll(new YmlTools.YmlParser().load("classpath:application-" + active + ".yml").getAll());
    }

    public String getProperty(String key) {
        return conf.get(key);
    }
}
三、测试

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值