关于java的property文件
2009-12-15 10:12
399 查看
以下是我在一个博客中看到的,感觉写的不错,希望对大家有所帮助:
这里暂且把XML格式放在一边吧,我们来看看Java使用的最多的.properties配置文件...
今天,看到好几个J2EE的应用发布到服务器上的时候,都要在J2EE Container启动的时候,在启动的脚本上面添加启动的参数:
这样一来,每每有新的应用需要发布到J2EE Applicaion Server的时候,又要来修改启动脚本,并作停机和重新启动的动作...给生产带来了很大的不便...
反过来看看目前流行的做法,比如说log4j、hibernate等等都有一个很好的解决办法,就是把对应的配置文件(某个.properties的文档),放在应用的classes目录下,相关的程序会自己去读取...
看了一下,其实就是利用ClassLoader.getSystemResourceAsStream()来实现的,因为对于某一个应用,所有Class的装载,都是由一系列存在父子关系的ClassLoader来完成的...通过上面这个方法,会递归查找所需要的properties文件...作了一个代码如下:
这样,就可以直接修改.properties文件,就可以生效了,不用重开服务器!而且,多个应用之间的配置都是隔离开的,每个配置文件都是随着自己所在的WAR绑定的!对 one AP Server <--> multi Applications 再好不过了!
自己给这个起了个名字,叫做“自适应配置文件读取”!
这里暂且把XML格式放在一边吧,我们来看看Java使用的最多的.properties配置文件...
今天,看到好几个J2EE的应用发布到服务器上的时候,都要在J2EE Container启动的时候,在启动的脚本上面添加启动的参数:
-DSystemAConfigFile="XXXXX" -DSystemBConfigFile="YYYYY" -DSystemCConfigFile="ZZZZZ" |
反过来看看目前流行的做法,比如说log4j、hibernate等等都有一个很好的解决办法,就是把对应的配置文件(某个.properties的文档),放在应用的classes目录下,相关的程序会自己去读取...
看了一下,其实就是利用ClassLoader.getSystemResourceAsStream()来实现的,因为对于某一个应用,所有Class的装载,都是由一系列存在父子关系的ClassLoader来完成的...通过上面这个方法,会递归查找所需要的properties文件...作了一个代码如下:
package net.csdn.blog.xport; import java.io.InputStream; import java.util.Properties; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; public class Configure { private static final Logger logger = LogManager.getLogger(Configure.class); private String propertiesFileName; private Properties propertiesTable; public Configure() { this.loadPropertiesTable(); } public Configure(String propertiesFileName) { this.setPropertiesFileName(propertiesFileName); this.loadPropertiesTable(); } public String getPropertiesFileName() { return this.propertiesFileName; } public void setPropertiesFileName(String propertiesFileName) { this.propertiesFileName = propertiesFileName; } private void loadPropertiesTable() { propertiesTable = new Properties(); if (this.propertiesFileName != null && this.propertiesFileName != "") { try { /* * 2005/11/14, 同事发现了此处存在Bug,只能在Console模式下运行,如果是在Web方式下 * 存在Bug...原因是此处使用了ClassLoader类别读取Resource,丢失了层级关系! * InputStream in = * ClassLoader.getSystemResourceAsStream(this.propertiesFileName); * propertiesTable.load(in); */ ClassLoader classLoader = Configure.class.getClassLoader(); URL resUrl = classLoader.getResource(this.propertiesFileName); /* * 虽然拿到了URL, 不好用new FileInputStream(URL.toString())来操作,因为在Windows * 和*NIX上面,对file:///协议的表示是不兼容的。java.net.URL会把Windows上面的文件 * 地址解释为file:/D:/xxx/,这样就错了,正确的应该是file:///D:/xxx ! * * 所以,既然有了URL对象,就可以直接openStream了! */ InputStream in = resUrl.openStream(); propertiesTable.load(in); } catch (Exception ex) { logger.debug(ex.getMessage(), ex); logger.info("can not load properties file:" + this.propertiesFileName); } } } public String getProperty(String propertyName) { return this.propertiesTable.getProperty(propertyName); } } |
自己给这个起了个名字,叫做“自适应配置文件读取”!
相关文章推荐
- 关于Java property 文件,我的一些看法(1)
- 关于Java property 文件,我的一些看法(2)
- java基础:关于java流与文件操作
- java:关于properties配置文件中的换行(多行)的坑
- 关于在做Java项目的时候如何把第三方的jar包一起打包成jar文件
- 关于java的文件操作
- 关于Java读取和编写BMP文件的总结
- java IO 关于创建文件的一个细节、技巧
- 关于 文件操作 【JAVA_IO】的学习与总结。
- 关于Java文件删除的操作
- 关于java读写文件的测试
- 关于将java程序打包成可执行exe文件过程需要注意事项
- 关于JAVA动态建立文件目录
- 关于Java Web 使用 iText 将数据库中的 表 的数据 生成 PDF 格式文件(升級版本)
- java中关于二进制文件的读写[二进制文件读写]
- 关于svn上传的代码每个java文件上都出现class问件暨代码编译路径的若干问题
- 关于idea运行项目时突然java文件报错
- 关于JAVA IO流中拷贝文件的几种方法
- 关于java文件编译后,同一个文件出现"$"的class
- java的property配置文件的用法