问题:
#两个jar包,a.jar, b.jar
#各有一个配置文件,同名同路径,都是conf/db.properties,内容*不*同。
#这俩jar包都会读取*其jar内*的此配置文件
问题是:会出现混淆问题么?
答案:
# 如何读取jar保内的配置文件。
# jvm的classloader加载文件的顺序。
# 加载.class文件和其他文件有区别吗?
其他:
一定要解决此问题的,否则多个module依赖会出现很大的问题。
工程:
a.jar
public class TestAaa { public static void main(String[] args) { TestAaa.readDbProp(); System.out.println("----"); TestAaa.scanProps(); } public static void readDbProp() { //返回读取指定资源的输入流 InputStream is = TestAaa.class.getResourceAsStream("/conf/db.properties"); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String s = ""; try { while ((s = br.readLine()) != null){ System.out.println(s); } } catch (IOException e) { e.printStackTrace(); } } public static void scanProps(){ Enumeration<URL> ps = null; URL url = null; try { ps = Thread.currentThread().getContextClassLoader().getResources("conf/db.properties"); System.out.println("ps.hasMoreElements()?" + ps.hasMoreElements()); // url = Thread.currentThread().getContextClassLoader().getResource("/conf/db.properties"); } catch (IOException e) { e.printStackTrace(); } while(ps.hasMoreElements()) { InputStream in = null; try { in = ps.nextElement().openStream(); Properties p = new Properties(); p.load(in); System.out.println(p.getProperty("username")); } catch (IOException e) { e.printStackTrace(); } finally { closeIO(in); } } } private static void closeIO(Closeable io) { if(io != null) { try { io.close(); } catch (IOException e) { e.printStackTrace(); } } } }
b.jar
public class TestAaa { public static void main(String[] args) { TestAaa.readDbProp(); System.out.println("----"); TestAaa.scanProps(); } public static void readDbProp() { //返回读取指定资源的输入流 InputStream is = TestAaa.class.getResourceAsStream("/conf/db.properties"); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String s = ""; try { while ((s = br.readLine()) != null){ System.out.println(s); } } catch (IOException e) { e.printStackTrace(); } } public static void scanProps(){ Enumeration<URL> ps = null; URL url = null; try { ps = Thread.currentThread().getContextClassLoader().getResources("conf/db.properties"); System.out.println("ps.hasMoreElements()?" + ps.hasMoreElements()); // url = Thread.currentThread().getContextClassLoader().getResource("/conf/db.properties"); } catch (IOException e) { e.printStackTrace(); } while(ps.hasMoreElements()) { InputStream in = null; try { in = ps.nextElement().openStream(); Properties p = new Properties(); p.load(in); System.out.println(p.getProperty("username")); } catch (IOException e) { e.printStackTrace(); } finally { closeIO(in); } } } private static void closeIO(Closeable io) { if(io != null) { try { io.close(); } catch (IOException e) { e.printStackTrace(); } } } }
main.jar
public class TestMain { public static void main(String[] args) { // TestBbb.readDbProp(); System.out.println("+++++++++++++++++++++"); TestAaa.readDbProp(); // TestBbb.scanProps(); System.out.println("+++++++++++++++++++++"); TestAaa.scanProps(); } }
测试的时候注意调整jar包的顺序。
# 可以读取多个同名配置文件
public static void scanProps(){ Enumeration<URL> ps = null; URL url = null; try { ps = Thread.currentThread().getContextClassLoader().getResources("conf/db.properties"); System.out.println("ps.hasMoreElements()?" + ps.hasMoreElements()); } catch (IOException e) { e.printStackTrace(); } while(ps.hasMoreElements()) { InputStream in = null; try { in = ps.nextElement().openStream(); Properties p = new Properties(); p.load(in); System.out.println(p.getProperty("username")); } catch (IOException e) { e.printStackTrace(); } finally { closeIO(in); } } }
# 读取单个和多个文件的路径写法
Thread.currentThread().getContextClassLoader().getResources("conf/db.properties");
TestBbb.class.getResourceAsStream("/conf/db.properties");
P
a
a
e
e
r
相关推荐
Eclipse打包成JAR包读取外部JAR包和外部配置文件 Eclipse 打包 jar 外部JAR 外部配置文件
这里我们以 gson.jar 为例,在 gson.jar 包目录下新建一个文本文件,名字随意,例如 rule.txt,写入下面的内容。 rule com.google.gson.** com.google.mygson.@1 上面写的就是修改规则,我们将包名中的 gson 修改为...
java读JAR包里的配置文件、java读JAR包里的配置文件
SpringMVC所有jar包和配置文件可以快速配置springmvc项目
配置文件,里面包含许多配置文件以及所需要的jar包
项目中,经常把一部分功能独立出来,做一个java project,然后打成jar包供其他项目引用...这时候,如果jar包中需要读取配置文件信息,则很少把该配置打进jar包,因为它不方便修改,更多都是采用jar包读取外部配置文件。
struts核心jar包及xml配置文件
数据库连接池的jar包,DataSourceFactory文件以及配置文件
commons-configuration.jar 解析配置文件(properties、xml)所需jar包
SpringMVC Spring MyBatis 框架整合需要的jar包 配置文件 工具类
这是采用springmvc进行开发时所需要用到的jar包及配置文件。在这里都达成了一个压缩包,方便使用
此文件内包含运行C3P0数据库连接池必要的jar包和配置文件。不懂如何使用的话请移步我的博客https://blog.csdn.net/zg97zb/article/details/103788880
ehcache缓存的jar包和配置文件。
这是一个springboot+maven写的demo,主要演示springboot项目通过maven插件,将依赖与配置文件打包到jar包外部,实现灵活的项目管理.仅供参考学习
hibernateJar包及配置文件
完整的c3p0jar包,直接创建lib目录导入就可以使用。并且有相应的配置文件,可以按照需求自行选择。
SpringMvc.rar jar包及配置文件
windows运行jar包指定jdk路径的脚本文件。可以不用系统的jdk运行,指定某个文件下的jdk运行jar包
log4j jar 包 和 其配置文件。可直接使用
SSM框架整合jar包和maven pom.xml配置文件整合