您的位置:首页 > 编程语言 > Java开发

谈Java Properties配置文件, 是J2EE容器的命令行绑定, 还是使用Deploy自适应的绑定?

2008-03-30 16:19 651 查看
这里暂且把XML格式放在一边吧,我们来看看Java使用的最多的.properties配置文件...

今天,看到好几个J2EE的应用发布到服务器上的时候,都要在J2EE Container启动的时候,在启动的脚本上面添加启动的参数:
 -DSystemAConfigFile="XXXXX" -DSystemBConfigFile="YYYYY" -DSystemCConfigFile="ZZZZZ"
这样一来,每每有新的应用需要发布到J2EE Applicaion Server的时候,又要来修改启动脚本,并作停机和重新启动的动作...给生产带来了很大的不便...

反过来看看目前流行的做法,比如说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 {

[b]  private[/b] static final Logger logger = LogManager.getLogger(Configure.class);

[b]  private[/b] String propertiesFileName;
[b]  private[/b] Properties propertiesTable;

[b]  public[/b] Configure() {
[b]    this[/b].loadPropertiesTable();
[b]  }[/b]

[b]  public[/b] Configure(String propertiesFileName) {
[b]  [b]  [/b]this[/b].setPropertiesFileName(propertiesFileName);
[b][b]    [/b]this[/b].loadPropertiesTable();
[b]  }[/b]

[b]  public[/b] String getPropertiesFileName() {
[b][b]    [/b]return[/b] this.propertiesFileName;
[b]  }[/b]

[b]  public[/b] void setPropertiesFileName(String propertiesFileName) {
[b]  [b]  [/b]this[/b].propertiesFileName = propertiesFileName;
[b]  }[/b]

[b]  private[/b] void loadPropertiesTable() {
  [b]  [/b]propertiesTable = new Properties();
[b]  [b]  [/b]if[/b] (this.propertiesFileName != null && this.propertiesFileName != "") {
[b][b]  [b]  [/b]  [/b]try[/b] {
        /*
         * 2005/11/14, 同事发现了此处存在Bug,只能在Console模式下运行,如果是在Web方式下
         * 存在Bug...原因是此处使用了ClassLoader类别读取Resource,丢失了层级关系!
[b]  [b]  [/b]  [/b]   * InputStream in =
[b]  [b]  [/b]  [/b]   *   ClassLoader.getSystemResourceAsStream(this.propertiesFileName);
[b]  [b]  [/b]  [/b]   * 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);
[b][b]  [b]  [/b]  [/b]}[/b]
[b][b]  [b]  [/b]  [/b]catch[/b] (Exception ex) {
  [b]    [/b]  logger.debug(ex.getMessage(), ex);
  [b]    [/b]  logger.info("can not load properties file:" + this.propertiesFileName);
[b][b]  [b]  [/b]  [/b]}[/b]
[b]  [b]  [/b]}[/b]
[b]  }[/b]

[b]  public[/b] String getProperty(String propertyName) {
[b]  [b]  [/b]return[/b] this.propertiesTable.getProperty(propertyName);
[b]  }[/b]

}
这样,就可以直接修改.properties文件,就可以生效了,不用重开服务器!而且,多个应用之间的配置都是隔离开的,每个配置文件都是随着自己所在的WAR绑定的!对 one AP Server <--> multi Applications 再好不过了!

自己给这个起了个名字,叫做“自适应配置文件读取”!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: