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);
}
}
spring 加载jar包中的配置文件的更多相关文章
- spring加载jar包中多个配置文件(转)
转自:http://evan0625.iteye.com/blog/1598366 在使用spring加载jar包中的配置文件时,不支持通配符,需要一个一个引入,如下所示: Java代码 <co ...
- spring加载jar包中多个配置文件
转自:http://www.cnblogs.com/GarfieldTom/p/3723915.html <import resource="classpath*:applicatio ...
- spring加载jar包中多个配置文件(转载)
本文转载自:http://www.cnblogs.com/GarfieldTom/p/3723915.html
- 动态加载jar包中的类(方式一)
嘛, 直接上代码 public static class TestClassLoader extends ClassLoader { @Override protected Class<?> ...
- idea中pom如何加载jar包依赖
1.需求分析 在特定需求的情况下,idea需要加载jar包,那么如何在idea中正确的配置jar依赖呢?今天博主就这个问题给大伙讲解下,希望对大伙有所帮助 2.实现方案①在工程src目录下新建l ...
- JAVA动态加载JAR包的实现
如何动态的加载这些驱动!不可能把所有的数据库驱动都集成到JAR包中吧?!于是动态加载驱动的JAR包就产生了!其实这些在做系统基础代码时,经常用到,只是一般我们没有机会去搞而已. 动态加载JAR包,使用 ...
- java动态加载jar包,并运行其中的类和方法
动态加载jar包,在实际开发中经常会需要用到,尤其涉及平台和业务的关系的时候,业务逻辑部分可以独立出去交给业务方管理,业务方只需要提供jar包,就能在平台上运行. 下面通过一个实例来直观演示: 第一: ...
- tomcat/Java指定加载jar包的路径
背景:部署的web站点,应用默认加载工程的/webapps/工程名/WEB-INF/lib下的jar包 但是我需要提供一个和web工程没关系的的jar包管理目录 解决方法: 执行java方法时 ...
- 动态加载jar包(二)
上次说的加载jar包,有几个问题没有解决: 1.如果项目包含了其他的jar包如何解决? 2.如何规范上传的jar包的类和方法? 下面就解决一下上面两个问题 一.首先编写被调用的类,这次使用maven工 ...
随机推荐
- 【转】在一个Job中同时写入多个HBase的table
在进行Map/Reduce时,有的业务需要在一个job中将数据写入到多个HBase的表中,下面是实现方式. 原文地址:http://lookfirst.com/2011/07/hbase-multit ...
- Maven 使用国内镜像
1 修改maven 的配置文件 settings.xml,添加阿里云的一个中央仓库. 2 找到maven 的配置文件,一般在 maven 安装目录 apache-maven-3.5.0\conf 文件 ...
- iOS7相机隐私判断
转自:http://borissun.iteye.com/blog/1992303 装了iOS7的ip5的隐私设置里多了相机这一项(ip4装iOS7就没有). 如果隐私里把你的app对应的相机给关了, ...
- Mac OS安装git
mac系统在AppStore里下载最新的Xcode,目前最新版本是Version 8.3.1 (8E1000a), 由于最新版的Xcode里已集成了git,所以下载后可直接在终端使用git了.
- [Windows Azure] How to use the Windows Azure Blob Storage Service in .NET
How to use the Windows Azure Blob Storage Service in .NET version 1.7 version 2.0 This guide will de ...
- signal函数的原型声明void (*signal(int signo, void (*fun(int))))(int)分析
转:http://blog.sina.com.cn/s/blog_4850a7880100hnam.html void (*signal(int signo, void (*fun(int))))(i ...
- Windows 安装 setuptools 和 feedparser
一.安装setuptools: 页面: https://pypi.python.org/pypi/setuptools#downloads 1.下载该zip文件,解压,例如:C:\setuptools ...
- 【Java】HashTable和HashMap区别
①继承不同 public class Hashtable extends Dictionary implements Map public class HashMap extends Abstract ...
- How To Set Up SSH Keys
How To Set Up SSH Keys.https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys--2
- postgresql相关命令
1,打开命令窗口: 2,查看数据库用户:\du 3,列出所有数据库名:\l或者SELECT datname FROM pg_database; 4,切换某个数据库下面的某个用户下面:\c 数据库名 用 ...