加载所有jar包下指定文件
加载所有jar包下指定文件:
如spring中加载 META-INF/spring.handlers 加载
org.springframework.core.io.support.PropertiesLoaderUtils#loadAllProperties(java.lang.String, java.lang.ClassLoader)
/**
* Load all properties from the specified class path resource
* (in ISO-8859-1 encoding), using the given class loader.
* <p>Merges properties if more than one resource of the same name
* found in the class path.
* @param resourceName the name of the class path resource
* @param classLoader the ClassLoader to use for loading
* (or {@code null} to use the default class loader)
* @return the populated Properties instance
* @throws IOException if loading failed
*/
public static Properties loadAllProperties(String resourceName, @Nullable ClassLoader classLoader) throws IOException {
Assert.notNull(resourceName, "Resource name must not be null");
ClassLoader classLoaderToUse = classLoader;
if (classLoaderToUse == null) {
classLoaderToUse = ClassUtils.getDefaultClassLoader();
}
Enumeration<URL> urls = (classLoaderToUse != null ? classLoaderToUse.getResources(resourceName) :
ClassLoader.getSystemResources(resourceName));
Properties props = new Properties();
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
URLConnection con = url.openConnection();
ResourceUtils.useCachesIfNecessary(con);
InputStream is = con.getInputStream();
try {
if (resourceName.endsWith(XML_FILE_EXTENSION)) {
props.loadFromXML(is);
}
else {
props.load(is);
}
}
finally {
is.close();
}
}
return props;
}
事例:
public static void main(String[] args) {
String handlerMappingsLocation = "META-INF/spring.handlers";
try {
Properties properties = PropertiesLoaderUtils.loadAllProperties(handlerMappingsLocation, BeanUtil.class.getClassLoader());
Enumeration<?> enumeration = properties.propertyNames();
while (enumeration.hasMoreElements()) {
String key = (String) enumeration.nextElement();
System.out.println(key);
}
} catch (IOException e) {
e.printStackTrace();
}
}
核心语法:
ClassLoader.getSystemResources(resourceName)
加载所有jar包下指定文件的更多相关文章
- Spark on Yarn运行时加载的jar包
spark on yarn运行时会加载的jar包有如下: spark-submit中指定的--jars $SPARK_HOME/jars下的jar包 yarn提供的jar包 spark-submit通 ...
- 自定义ClassLoader,用于加载用户JAR包
最近在考虑C/S结构程序的软件自动升级的实现方式,比如QQ.飞信等都自动升级的功能. 自动升级模块虽然还没有编码完成,但是思路还是比较清晰的. 自动升级过程中,升级文件的JAR包是专门加载到程序中去的 ...
- 服务器程序动态加载自定义jar包的过程
需求: 用过hive的都知道,可以自定义hive的一个udf jar,然后将这个jar add到hive服务端,就会加载这个jar实现用户自定义逻辑.现在的需求就是实现这么一个服务端所做的事情! 场景 ...
- (转)自定义ClassLoader ----可以加载第三方jar包
package com.classloader.util; import java.io.IOException; import java.net.MalformedURLException; imp ...
- maven加载本地jar包到repository
maven加载本地jar到repository 这是一个常见场景,此处以本地opencv jar文件导入repository为例 1.Ubuntu下 mvn install:install-file ...
- 27 Java动态加载第三方jar包中的类
我加载的方法是://参数fileName是jar包的路径,processorName 是业务类的包名+类名public static A load(String fileName, String pr ...
- maven无法加载本地jar包以及maven项目打包后本地jar包没有打进项目的问题解决办法
1.首先设置依赖项,这样maven就会把该路径下的jar包导入项目引用 <dependency> <groupId>DPSDK-Manager</groupId> ...
- Gradle加载本地jar包
有时,我们需要的jar包不一定能在远程仓库中找到,这时我们需要加载本地的jar包. 加载单独的jar包 在项目底下添加libs目录,将jar包仍进libs目录 build.gradle配置如下: de ...
- 关于用wkwebview加载沙盒documents下html文件 模拟器可以,真机不行的解决方案
最近也遇到这个问题,把我解决的思路记录一下 1.问题: 用wkwebview加载(loadRequest)沙盒documents下html文件 模拟器可以,真机不行 (前提是html内部含引用外联样式 ...
随机推荐
- BZOJ.2597.[WC2007]剪刀石头布(费用流zkw)
BZOJ 洛谷 \(Description\) 给定一张部分边方向已确定的竞赛图.你需要给剩下的边确定方向,使得图中的三元环数量最多. \(n\leq100\). \(Solution\) 这种选择之 ...
- [CC-CMPVIRS]Computer Virus
[CC-CMPVIRS]Computer Virus 题目大意: 有一张纸带,从左到右被分成了\(n(n\le10^7)\)个格子,在刚开始,第\(i\)个格子上写着数字\(i\).这张纸带被分成了从 ...
- [HAOI2015]树上染色
Description: 有一棵点数为 N 的树,树边有边权.给你一个在 0~ N 之内的正整数 K ,你要在这棵树中选择 K个点,将其染成黑色,并将其他 的N-K个点染成白色 . 将所有点染色后,你 ...
- incomplete type is not allowed ofstream
错误: incomplete type is not allowed 解决方案: #include<fstream>
- BZOJ3252攻略——长链剖分+贪心
题目描述 题目简述:树版[k取方格数] 众所周知,桂木桂马是攻略之神,开启攻略之神模式后,他可以同时攻略k部游戏.今天他得到了一款新游戏<XX 半岛>,这款游戏有n个场景(scene),某 ...
- js将时间戳格式化为HH:ii:ss的格式
将时间戳格式化为 HH:ii:ss的格式 <html> <head> </head> <body> <span id="time&quo ...
- Java 多线程 重入锁
作为关键字synchronized的替代品(或者说是增强版),重入锁是synchronized的功能扩展.在JDK 1.5的早期版本中,重入锁的性能远远好于synchronized,但从JDK 1.6 ...
- Seaborn入门
Seaborn入门 Seaborn是基于matplotlib的python数据可视化库,提供更高层次的API封装,使用起来更加方便快捷. displot displot()集成了hist直方图和kde ...
- AES 加密问题
C# 里面封装的Aes算法好像跟网上C++的加密算法差很多.在网上找了很多资料才看到一个很早的文章, 用C#实现网上C++的算法. http://msdn.microsoft.com/zh-cn/ma ...
- sencha cmd 创建项目
一.软件支持 1.下载并解压Sencha Touch(浏览器支持Chrome.Safari.Internet Explorer 10或11.) 2.Sencha Cmd(Sencha Touch 2. ...