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

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. 【动态规划】bzoj1613 [Usaco2007 Jan]Running贝茜的晨练计划

    #include<cstdio> #include<algorithm> using namespace std; #define N 10001 int n,m,a[N],f ...

  2. 【数论】【枚举约数】【欧拉函数】bzoj2705 [SDOI2012]Longge的问题

    ∵∑gcd(i, N)(1<=i <=N) =k1*s(f1)+k2*s(k2)+...+km*s(km) {ki是N的约数,s(ki)是满足gcd(x,N)=ki(1<=x< ...

  3. 【莫比乌斯反演】BZOJ2920-YY的GCD

    [题目大意] 给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对. [思路] 太神了这道题……蒟蒻只能放放题解:戳,明早再过来看看 ...

  4. js创建json对象

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. 长按事件OnLongClickListener

    1.MainActivity.java package com.example.administrator.hello4; import android.support.v7.app.AppCompa ...

  6. iOS 灵活,简易,扩展性强的气泡提示框LFBubbleView(含源码)

    一.效果图 二.使用方法 使用简单,4行代码集成. _bubbleView = [[LFBubbleView alloc] initWithFrame:CGRectMake(, , , )]; _bu ...

  7. iOS 简单易用的二维码扫描及生成二维码三方控件LFQRCode,可灵活自定义UI

    一.扫码 扫描的控件是一个view,使用者只需贴在自己的控制器内即可.其他UI用户可在自己控制器随便添加.代码如下 - (void)viewDidLoad { [super viewDidLoad]; ...

  8. .NET反编译

    http://www.cnblogs.com/powertoolsteam/archive/2011/01/05/1926066.html

  9. 【java】子类可以通过调用父类的public方法调用父类的private方法,为什么?

    代码1: 打印结果: 代码2: 运行结果: 问题: 代码1中super是父类自己调用自己的add()方法,并在add()方法中调用了私有的del()方法,那为什么打印出来的this是子类? 代码2中t ...

  10. String的解析

    String作为Java中最常用的引用类型,相对来说基本上都比较熟悉,无论在平时的编码过程中还是在笔试面试中,String都很受到青睐,然而,在使用String过程中,又有较多需要注意的细节之处. 1 ...