Java配置文件读取和路径设置
记录几种读取配置文件的方法,以及配置文件的放置路径。
1、使用PropertiesLoaderUtils工具类(springframework包提供)
优点:实时加载配置文件,修改后立即生效,不必重启
配置文件至于classpath中(与class文件放在一起,如果打jar包需打到包内),代码如下:
private static void springUtil(){
Properties props = new Properties();
while(true){
try {
props=PropertiesLoaderUtils.loadAllProperties("param.properties");
for(Object key:props.keySet()){
System.out.print(key+":");
System.out.println(props.get(key));
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
try {Thread.sleep();} catch (InterruptedException e) {e.printStackTrace();}
}
}
2、根据文件路径读取
优点:配置文件可以放在jar包外面,根据文件路径寻找配置文件
代码如下:
public static String readValue(String fileName, String key)
{
Properties props = new Properties();
String value = null;
try
{
// 配置文件位于当前目录中的config目录下
InputStream in = new BufferedInputStream(new FileInputStream("config/" + fileName));
props.load(in);
value = props.getProperty(key);
}
catch (Exception e)
{
e.printStackTrace();
}
return value;
}
3、spring加载配置文件的两种方式
1)按classpath加载(配置文件与class文件放于同一目录)
初始化Spring代码:
public static boolean initialize() {
if (isInitialize) {
return true;
}
try {
appContenxt = new FileSystemXmlApplicationContext("spring.xml");
isInitialize = true;
return true;
}
catch (Exception e) {
logger.error("Initialize spring framework failed.", e);
return false;
}
}
配置文件格式:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>jdbc.properties</value>
<value>param.properties</value>
</list>
</property>
</bean>
ibatis配置写法:
<sqlMapConfig>
<settings cacheModelsEnabled="true" enhancementEnabled="true"
lazyLoadingEnabled="true" errorTracingEnabled="true" maxRequests=""
maxSessions="" maxTransactions="" useStatementNamespaces="true" />
<sqlMap resource="sqlmap/sqlmap-global.xml" />
<sqlMap resource="sqlmap/sqlmap-memo.xml" />
<sqlMap resource="sqlmap/sqlmap-city.xml" />
</sqlMapConfig>
2)按文件路径加载(比如配置文件位于当前目录中的config目录下)
初始化Spring代码:
public static boolean initialize() {
if (isInitialize) {
return true;
}
try {
appContenxt = new FileSystemXmlApplicationContext("file:config/spring.xml");
isInitialize = true;
return true;
}
catch (Exception e) {
logger.error("Initialize spring framework failed.", e);
return false;
}
}
配置文件格式:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>file:config/jdbc.properties</value>
<value>file:config/param.properties</value>
</list>
</property>
</bean>
ibatis配置写法:
<sqlMapConfig>
<settings cacheModelsEnabled="true" enhancementEnabled="true"
lazyLoadingEnabled="true" errorTracingEnabled="true" maxRequests=""
maxSessions="" maxTransactions="" useStatementNamespaces="true" />
<sqlMap url="file:config/sqlmap/sqlmap-global.xml" />
<sqlMap url="file:config/sqlmap/sqlmap-windcustomcode.xml" />
<sqlMap url="file:config/sqlmap/sqlmap-shiborprices.xml" />
</sqlMapConfig>
Java配置文件读取和路径设置的更多相关文章
- Java递归读取文件路径下所有文件名称并保存为Txt文档
本文用递归的方法实现读取一个路径下面的所有文件并将文件名称保存到Txt文件中,亲测可用. 递归读取文件路径下的所有文件: /** * 递归读取文件路径下的所有文件 * * @param path * ...
- Java配置文件读取中文乱码问题
背景 这是我之前在做的用友服务对接开发,昨天领导拿给财务测试时告诉我有乱码,当时我第一想法是用友那边的编码格式有问题,因为还在做其他任务,我说等问一下用友他们用的什么编码格式我们这边改一下,然后今天早 ...
- java 配置文件读取
1.getResourceAsStream Class.getClassLoader.getResourceAsStream(String path) :默认则是从ClassPath根下获取,path ...
- 转载:Java项目读取配置文件时,FileNotFoundException 系统找不到指定的文件,System.getProperty("user.dir")的理解
唉,读取个文件,也就是在项目里面去获得配置文件的目录,然后,变成文件,有事没事,总是出个 FileNotFoundException 系统找不到指定的文件,气死人啦. 还有就是:System.get ...
- Java学习-023-Properties 类 XML 配置文件读取及写入源代码
之前的几篇 Properties 文章已经讲述过了 Java 配置文件类 Properties 的基本用法,查看 JDK 的帮助文档时,也可看到在 Properties 类中还有两个方法 loadFr ...
- Java配置文件Properties的读取、写入与更新操作
/** * 实现对Java配置文件Properties的读取.写入与更新操作 */ package test; import java.io.BufferedInputStream; import j ...
- 对Java配置文件Properties的读取、写入与更新操作
http://breezylee.iteye.com/blog/1340868 对Java配置文件Properties的读取.写入与更新操作 博客分类: javase properties 对Jav ...
- Java工程读取resources中资源文件路径问题
正常在Java工程中读取某路径下的文件时,可以采用绝对路径和相对路径,绝对路径没什么好说的,相对路径,即相对于当前类的路径.在本地工程和服务器中读取文件的方式有所不同,以下图配置文件为例. 本地读取资 ...
- 实现对Java配置文件Properties的读取、写入与更新操作
/** * 实现对Java配置文件Properties的读取.写入与更新操作 */ package test; import java.io.BufferedInputStream; import j ...
随机推荐
- AOJ 2251 Merry Christmas (最小点覆盖)
[题目链接] http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2251 [题目大意] 给出一张图,现在有一些任务,要求在ti时刻送礼物 ...
- Java高级架构师(一)第11节:Mybatis的分页实现
- iOS开源项目阅读整理
精读过的开源项目,随时整理,随时更新,本文只记录项目地址,名称和内容,不发表心得. 1.AFNetWorking iOS人都知道,不细诉. 2.iCarousel 旋转木马,选项卡很不错的UI解决方案 ...
- 选择改变事件OnCheckedChange
1.效果图:选择正确的提示选对,选择错误提示选错 2.activity_main.xml <?xml version="1.0" encoding="utf-8&q ...
- 优化apk
1.首先找到Sdk的位置 2.在电脑中找到Sdk之后点击->bulid-tools 3.点击23.0.3,将需要优化的apk复制到23.0.3的目录下(比如aa.apk) 4.回到23.0.3之 ...
- Sublime text JsFormat插件的安装
javascript格式化插件JsFormat 1.下载这插件包 https://github.com/jdc0589/JsFormat 2.点击菜单:Preferences->Browse P ...
- Oracle查询库中记录数大于2千万的所有表
Oracle查询库中记录数大于2千万的所有表 假如当前用户拥有select any table权限,则可以使用下列sql语句: select table_name, num_rows from dba ...
- 纯JS操作获取桌面路径方法
//active 控件获取当前用户的桌面的路径的方法 var wsh = new ActiveXObject("wscript.shell"); listall(wsh.Speci ...
- Android动态载入Dex机制解析
1.什么是类载入器? 类载入器(class loader)是 Java™中的一个非常重要的概念.类载入器负责载入 Java 类的字节代码到 Java 虚拟机中. Java 虚拟机使用 Java 类的方 ...
- ExtentReports 结合 TestNg 生成自动化 html 报告 (支持多 suite)
转载:https://testerhome.com/topics/8134 重要说明:报告监听器源码修复一些bug,不再此处更新代码,最新代码可以到github查看最新报告监听器源码 前几天分享了ht ...