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

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. [BZOJ 2395] Time is money

    Link: BZOJ 2395 传送门 Solution: 算是一类比较经典的模型: 即对于一类经典问题,每点由1个权值化为2个权值,最终求$sigma(val_1)*sigma(val_2)$ 对于 ...

  2. 【费用流】【Next Array】费用流模板(spfa版)

    #include<cstdio> #include<algorithm> #include<cstring> #include<queue> using ...

  3. 前端基础-HTML简介及发展史

    一 HTML简介 二 HTML发展史 一. HTML简介 用户使用浏览器打开网页看到结果的过程就是:浏览器将服务端的文本文件(即网页文件)内容下载到本地,然后打开显示的过程. 而文本文件的文档结构只有 ...

  4. Mybatis添加用户&&Mybatis添加用户返回ID

    (1)添加用户(添加User记录) <!--插入用户 --> <insert id="insertUser" parameterType="com.so ...

  5. no such file or directory : 'users/shikx/xxx/xxx/Appirater.m'

    删除此处红色的.m文件即可

  6. 【spring data jpa】jpa实现update操作 字段有值就更新,没值就用原来的

    示例代码如下: /** *复杂JPA操作 使用@Query()自定义sql语句 根据业务id UId去更新整个实体 * 删除和更新操作,需要@Modifying和@Transactional注解的支持 ...

  7. 集合视图UICollectionView 介绍及其示例程序

    UICollectionView是一种新的数据展示方式,简单来说可以把它理解成多列的UITableView.如果你用过iBooks的话,可 能你还对书架布局有一定印象,一个虚拟书架上放着你下载和购买的 ...

  8. Kafka server.properties配置,集群部署

    server.properties中所有配置参数说明(解释) broker.id =0每一个broker在集群中的唯一表示,要求是正数.当该服务器的IP地址发生改变时,broker.id没有变化,则不 ...

  9. 补番计划 (长沙理工大学第十一届程序设计竞赛)(双端队列+set容器+string)

    补番计划 Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other) Total Submissi ...

  10. Android中Service概述

    Service是Android中一种非常重要的组件,一般来说有两种用途:用Service执行长期执行的操作,而且与用户没有UI界面的交互:某个应用程序的Service能够被其它应用程序的组件调用以便提 ...