记录几种读取配置文件的方法,以及配置文件的放置路径。

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配置文件读取和路径设置的更多相关文章

  1. Java递归读取文件路径下所有文件名称并保存为Txt文档

    本文用递归的方法实现读取一个路径下面的所有文件并将文件名称保存到Txt文件中,亲测可用. 递归读取文件路径下的所有文件: /** * 递归读取文件路径下的所有文件 * * @param path * ...

  2. Java配置文件读取中文乱码问题

    背景 这是我之前在做的用友服务对接开发,昨天领导拿给财务测试时告诉我有乱码,当时我第一想法是用友那边的编码格式有问题,因为还在做其他任务,我说等问一下用友他们用的什么编码格式我们这边改一下,然后今天早 ...

  3. java 配置文件读取

    1.getResourceAsStream Class.getClassLoader.getResourceAsStream(String path) :默认则是从ClassPath根下获取,path ...

  4. 转载:Java项目读取配置文件时,FileNotFoundException 系统找不到指定的文件,System.getProperty("user.dir")的理解

    唉,读取个文件,也就是在项目里面去获得配置文件的目录,然后,变成文件,有事没事,总是出个 FileNotFoundException  系统找不到指定的文件,气死人啦. 还有就是:System.get ...

  5. Java学习-023-Properties 类 XML 配置文件读取及写入源代码

    之前的几篇 Properties 文章已经讲述过了 Java 配置文件类 Properties 的基本用法,查看 JDK 的帮助文档时,也可看到在 Properties 类中还有两个方法 loadFr ...

  6. Java配置文件Properties的读取、写入与更新操作

    /** * 实现对Java配置文件Properties的读取.写入与更新操作 */ package test; import java.io.BufferedInputStream; import j ...

  7. 对Java配置文件Properties的读取、写入与更新操作

    http://breezylee.iteye.com/blog/1340868 对Java配置文件Properties的读取.写入与更新操作 博客分类: javase properties  对Jav ...

  8. Java工程读取resources中资源文件路径问题

    正常在Java工程中读取某路径下的文件时,可以采用绝对路径和相对路径,绝对路径没什么好说的,相对路径,即相对于当前类的路径.在本地工程和服务器中读取文件的方式有所不同,以下图配置文件为例. 本地读取资 ...

  9. 实现对Java配置文件Properties的读取、写入与更新操作

    /** * 实现对Java配置文件Properties的读取.写入与更新操作 */ package test; import java.io.BufferedInputStream; import j ...

随机推荐

  1. 【二分】【动态规划】Codeforces Round #393 (Div. 1) B. Travel Card

    水dp,加个二分就行,自己看代码. B. Travel Card time limit per test 2 seconds memory limit per test 256 megabytes i ...

  2. Djanog|requirements.txt生成

    Django | requirement.txt 生成 pip django 1   pip 通常我们熟悉使用的都是 pip, 这个工具确实方便项目管理依赖包.当想把当前项目依赖的包的名称和版本导入指 ...

  3. 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法

    sort函数在使用中非常好用,也非常简单,而且效率与冒泡或者选择排序不是一个数量级.本文就sort函数在vector中的用法分为sort函数入门用法与自定义comp比较函数比较结构体这两个最基本的功能 ...

  4. OC语言基础之NSString

    1.字符串的创建 1: NSString *s1 = @"jack"; 2: 3: //NSString *s2 = [[NSString alloc] initWithStrin ...

  5. 【译】PHP中的Session及其一些安全措施

    有一点我们必须承认,大多数web应用程序都离不开session的使用.这篇文章将会结合php以及http协议来分析如何建立一个安全的会话管理机制.我们先简单的了解一些http的知识,从而理解该协议的无 ...

  6. TCP Socket一些东西

    1. 若connect失败该套接字不可再用,必须close当前套接字,重新调用socket. 手册上注明连接失败后, socket的状态是未知的, 所以再次connect, 可能成功, 也可能失败. ...

  7. 【mybatis】mybatis 中select 查询 select * 查询出来的数据,字段值带不出来 数据不全

    原来的代码如下: <select id="findByGoodsUid" resultType="com.pisen.cloud.luna.ms.goods.bas ...

  8. ODATA4 及实现

    ODATA4 的JAVASCRIPT 实现:     http://jaydata.org/ ODATA4 的JAVA 项目  Apache Olingo:http://olingo.incubato ...

  9. Linux中C语言的编程

    编译的过程 编译的概念:编译程序读取源程序(字符流),对之进行词法与语法的分析,将高级语言指令转换成功能等效的汇编代码,再由汇编程序转换成机器语言,并且按照操作系统对可执行文件格式的要求链接成可执行程 ...

  10. Yii2系列教程二:MVC,Forms和Layouts

    上一篇文章我们简单地实现了Yii2框架安装和Hello World,而在这一篇文章当中,我们会带着好奇之心去探索一下在Yii2中的几个重要的元素组成:MVC,Forms和Layouts. 本文的目标是 ...