zoukankan      html  css  js  c++  java
  • spring 加载jar包中的配置文件

    package com.xxx.ssptsppt.dataexchange.utils;
    
    import com.xxx.maybee.engine.utils.FileUtil;
    import com.xxx.maybee.engine.utils.PubString;
    import org.springframework.core.io.Resource;
    import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
    import org.springframework.core.io.support.ResourcePatternResolver;
    
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.HashMap;
    import java.util.Map;
    
    
    public class Confs {
        private static Map<String, File> confFiles;
    
        static {
            confFiles = new HashMap<>();
            String tmpDir = "tmpconf/";
            ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
            try {
                Resource[] resources = resolver.getResources("/*.json");
                for (Resource resource : resources) {
                    String confName = resource.getFilename();
                    InputStream confIs = resource.getInputStream();
                    File confFile = extractFile(confName, confIs, tmpDir);
                    confFiles.put(confName, confFile);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        private static File extractFile(String confName, InputStream confIs, String tmpDir) {
            File tmpConfDir = new File(tmpDir);
            if (!tmpConfDir.exists()) {
                boolean mkdirs = tmpConfDir.mkdirs();
                if (!mkdirs) {
                    throw new RuntimeException("临时配置文件无法创建!");
                }
            }
    
            File confFilePath = new File(tmpConfDir, confName);
            try {
                FileUtil.saveToFile(confIs, confFilePath, true, false);
            } catch (IOException e) {
                FileUtil.delFolder(tmpDir);
                throw new RuntimeException("配置文件提取失败!");
            }
    
            return confFilePath;
        }
    
        public static File getConf(String confFileName) {
            if (PubString.isNullOrSpace(confFileName)) {
                return null;
            }
            return confFiles.get(confFileName);
        }
    }
    

      

  • 相关阅读:
    去除aspx生成的页面最开始的空行
    单个方框内图片垂直水平居中和等比例缩小(支持所有浏览器)
    .net里怎样在Main方法之前执行代码?
    WIN7系统盘空间不够用解决办法
    语音识别工具箱之HTK安装与使用
    一个VC中的DLL导出类的例子
    c++ 随机数 产生不重复的随机数
    android开发 NDK 编译和使用静态库、动态库
    c++指针 初识
    c++ UTF8和Unicode 互转
  • 原文地址:https://www.cnblogs.com/hfultrastrong/p/9474249.html
Copyright © 2011-2022 走看看