commons configuration管理项目的配置文件

Commons Configutation  

commons configuration可以很方便的访问配置文件和xml文件中的的内容。Commons Configuration 是为了提供对属性文件、XML文件、JNDI资源、来自JDBC Datasource数据的访问。

  官方文档:http://commons.apache.org/proper/commons-configuration/

1、Maven中引入相关的jar

复制代码
        <!-- 配置文件读取 -->
        <dependency>
            <groupId>commons-configuration</groupId>
            <artifactId>commons-configuration</artifactId>
            <version>1.5</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.10</version>
        </dependency>
复制代码

2、读取properties文件的内容

(1)新建一个icp.properties

name=parry
port=21
flag=true
users=Tom,parry

(2)工具类读取

复制代码
package icp;

import java.util.List;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;

public class Test {

    public static final String fileName = "icp.properties";

    public static PropertiesConfiguration cfg = null;

    static {
        try {
            cfg = new PropertiesConfiguration(fileName);
        } catch (ConfigurationException e) {
            e.printStackTrace();
        }
        // 当文件的内容发生改变时,配置对象也会刷新
        cfg.setReloadingStrategy(new FileChangedReloadingStrategy());
    }
  // 读String
    public static String getStringValue(String key) {
        return cfg.getString(key);
    }
  // 读int
    public static int getIntValue(String key) {
        return cfg.getInt(key);
    }
  // 读boolean
    public static boolean getBooleanValue(String key) {
        return cfg.getBoolean(key);
    }
  // 读List
    public static List<?> getListValue(String key) {
        return cfg.getList(key);
    }
  // 读数组
    public static String[] getArrayValue(String key) {
        return cfg.getStringArray(key);
    }

}
复制代码

(3)测试

复制代码
public static void main(String[] args) {
        String name = Test.getStringValue("name");
        System.out.println("name:" + name);
        int port = Test.getIntValue("port");
        System.out.println("port:" + port);
        boolean flag = Test.getBooleanValue("flag");
        System.out.println("flag:" + flag);
        List<String> users = (List<String>) Test.getListValue("users");
        for (String user : users) {
            System.out.println("user:" + user);
        }
    }
复制代码

(4)测试结果

3、读取XML配置文件

(1)新建一个XMl文件

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<config>
    <database>
        <url>127.0.0.1</url>
        <port>1521</port>
        <login>admin</login>
        <password>pass</password>
    </database>
</config>
复制代码

(2)读取XML配置的工具文件

复制代码
package icp;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;

public class XmlTest {

    public static final String fileName = "XMLProperties.xml";

    public static XMLConfiguration cfg = null;

    static {
        try {
            cfg = new XMLConfiguration(fileName);
        } catch (ConfigurationException e) {
            e.printStackTrace();
        }
        // 配置文件 发生变化就重新加载
        cfg.setReloadingStrategy(new FileChangedReloadingStrategy());
    }

    public static String getStringValue(String key) {
        return cfg.getString(key);
    }

    public static int getIntValue(String key) {
        return cfg.getInt(key);
    }
}
复制代码

  这里只是添加读取String 和读取Int的方法,其他的方法类似。

(3)测试

复制代码
    public static void main(String[] args) {
        String url = XmlTest.getStringValue("database.url");
        System.out.println("url:" + url);
        int port =XmlTest.getIntValue("database.port");
        System.out.println("port:"+port);
    }
复制代码

(4)测试结果

由于项目中,经常有多个配置文件,最后提供一个工具类:

复制代码
public class PropertiesUtil {

    public static Map<String, Object> configMap = new ConcurrentHashMap<String, Object>();

    public static String getStringValue(String fileName, String key) {
        if (!configMap.containsKey(key)) {
            PropertiesUtil.initConfig(fileName);
        }
        if (fileName.endsWith(".properties")) {
            PropertiesConfiguration cfg = (PropertiesConfiguration) configMap.get(fileName);
            return cfg.getString(key);
        } else if (fileName.endsWith(".xml")) {
            XMLConfiguration cfg = (XMLConfiguration) configMap.get(fileName);
            return cfg.getString(key);
        }
        return null;
    }

    private static void initConfig(String fileName) {
        try {
            if (fileName.endsWith(".xml")) {
                XMLConfiguration cfg = new XMLConfiguration(fileName);
                configMap.put(fileName, cfg);
            } else if (fileName.endsWith(".properties")) {
                PropertiesConfiguration cfg = new PropertiesConfiguration(fileName);
                configMap.put(fileName, cfg);
            }
        } catch (ConfigurationException e) {
            e.printStackTrace();
        }
    }
}
复制代码

测试:

复制代码
    public static void main(String[] args) {
        String name= PropertiesUtil.getStringValue("icp.properties", "name");
        System.out.println(name);
        String url = PropertiesUtil.getStringValue("icp.xml", "database.url");
        System.out.println(url);
    }
复制代码
posted on 2017-10-10 19:25  滴水之光  阅读(255)  评论(0编辑  收藏  举报