configuration

桃扇骨 2022-06-10 09:22 511阅读 0赞

Apache commons-configuration 是一个开源组件,可以方便的对项目中的配置文件进行读取和保存,本文只说明如何使用该组件读取项目中的配置文件(以ini、properties、xml文件为例),保存另文描述。

首先,需要添加相关的jar,我使用的是1.7版本,利用maven导入jar包:


commons-configuration
commons-configuration
1.7

除了commons-configuration.jar以外,maven会自动添加其依赖的相关jar,如commons-collections、commons-lang等。

注意,如果要使用XML作为项目配置文件,并且需要使用XPath对配置文件进行查询的话,还需要添加commons-jxpath组件,利用maven导入jar包:


commons-jxpath
commons-jxpath
1.3


在src/main/resources下新建一个类,命名为ConfigApp;

在src/main/resources下新建一个config文件夹,在config文件夹中新建三个配置文件:

cfg .ini (ini文件的简述,百度百科:baike.baidu.com/view/509647.htm)

[plain] view plain copy

  1. [os]
  2. edition = windows7

cfg.properites

[plain] view plain copy

  1. platform.jre = 1.7

cfg.xml

[html] view plain copy

  1. <?**xml version=”1.0” encoding=”UTF-8”?>**
  2. <**college**>
  3. <**student name=”foo” gender=”M”>**
  4. <**score course=”Algorithm”>97</score>**
  5. <**score course=”Operating System”>97</score>**
  6. </**student**>
  7. <**student name=”bar” gender=”F”>**
  8. <**score course=”Algorithm”>86</score>**
  9. <**score course=”Operating System”>91</score>**
  10. </**student**>
  11. <**teacher**>
  12. <**name**>tee</**name**>
  13. <**age**>31</**age**>
  14. </**teacher**>
  15. </**college**>

(1)使用commons-configuration读取properites文件

properties文件是Java平台默认的配置文件格式,其优点是格式清晰,简单易懂,使用commons-configuration读取properties文件也比较简单,代码如下:

[java] view plain copy

  1. public static void readProperties() throws ConfigurationException {
  2. PropertiesConfiguration pcfg = new PropertiesConfiguration(“config/cfg.properties”);
  3. System.out.println(pcfg.getString(“platform.jre”));
  4. }

注意,这里的路径一定要搞清楚,配置文件是放在config文件夹中的;

(2)使用commons-configuration读取ini文件

ini文件是windows平台上的一种配置文件格式,之所以将读取这种配置文件写在这里是因为:在Eclipse中编辑properties文件时,中文会显示为乱码,但是ini文件不会。读取INI文件的代码也非常简单,代码如下:

[java] view plain copy

  1. public static void readFromIni() throws ConfigurationException {
  2. HierarchicalINIConfiguration ini = new HierarchicalINIConfiguration(“config/cfg.ini”);
  3. System.out.println(“2. read configuration from cfg.ini…”);
  4. System.out.println(“os.edition = “ + ini.getString(“os.edition”));
  5. System.out.println();
  6. }

(3)使用commons-configuration读取xml文件

xml是最常用与配置文件,commons-configuration对xml的支持也非常完善,其读取xml文件有两种方式,一种是简单化的,使用“.”作为分隔符,如果配置文件较简单,可以使用这种方式,另一种是使用XPath作为查询方法,在配置文件中进行查询后返回结果,这种方式一般用于较复杂的配置文件。读取xml配置文件的代码如下:

[java] view plain copy

  1. public static void readFromXml() throws ConfigurationException {
  2. XMLConfiguration xml = new XMLConfiguration(“config/cfg.xml”);
  3. System.out.println(“3. read configuration from cfg.xml…”);
  4. // XML配置文件的简单使用(不使用XPath)
  5. System.out.println(“3.1 simple use..”);
  6. System.out.println(“teacher’s name = “ + xml.getString(“teacher.name”));
  7. System.out.println(“teacher’s age = “ + xml.getString(“teacher.age”));
  8. // 使用XPath查询配置文件
  9. System.out.println(“3.2 use xpath…”);
  10. xml.setExpressionEngine(new XPathExpressionEngine());
  11. // 查询姓名为foo的学生的算法课成绩
  12. String expr1 = “/student[@name=’foo’]/score[@course=’Algorithm’]“;
  13. System.out.println(“foo’s algorithm score = “ + xml.getString(expr1));
  14. // 查询所有学生的算法课成绩
  15. String expr2 = “/student//score[@course=’Algorithm’]“;
  16. System.out.println(“all students algorithm’s score = “ + Arrays.toString(xml.getStringArray(expr2)));
  17. // 查询姓名为foo的学生的性别
  18. String expr3 = “/student[@name=’foo’]/@gender”;
  19. System.out.println(“foo’s gender = “ + xml.getString(expr3));
  20. System.out.println();

需要注意的是,如果需要使用XPath进行查询,应该先执行

[java] view plain copy

  1. xml.setExpressionEngine(new XPathExpressionEngine());

方法。

(4)组合配置

目前,一般大点的项目都会有多个配置文件,将不同的配置项分类存储于多个配置文件中,这样必然会在项目中包含多个配置类,编写代码与维护都不方便。commons-configuration提供了一种配置组合机制。可以将所有的配置对象组合于一个CompositeConfiguration对象,这样就可以将若干个配置对象组合到一起,只对该组合配置对象进行操作即可。主要代码如下:

[java] view plain copy

  1. public static void readFromCompositeConfig() throws ConfigurationException {
  2. System.out.println(“4. composite all configurations…”);
  3. CompositeConfiguration configurations = new CompositeConfiguration();
  4. HierarchicalINIConfiguration ini = new HierarchicalINIConfiguration(“config/cfg.ini”);
  5. PropertiesConfiguration props = new PropertiesConfiguration(“config/cfg.properties”);
  6. XMLConfiguration xml = new XMLConfiguration(“config/cfg.xml”);
  7. xml.setExpressionEngine(new XPathExpressionEngine());
  8. configurations.addConfiguration(ini);
  9. System.out.println(“add ini to composite configuration…”);
  10. configurations.addConfiguration(props);
  11. System.out.println(“add properties to composite configuration…”);
  12. configurations.addConfiguration(xml);
  13. System.out.println(“add xml to composite configuration…”);
  14. System.out.println(“platform.jre = “ + configurations.getString(“platform.jre”));
  15. System.out.println(“os.edition = “ + configurations.getString(“os.edition”));
  16. String expr1 = “/student[@name=’foo’]/score[@course=’Algorithm’]“;
  17. System.out.println(“foo’s algorithm score = “ + configurations.getString(expr1));
  18. }

其实使用方法非常简单,首先根据需求生成配置对象,一个配置对象一般对应一个配置文件,然后生成一个CompositeConfiguration对象,将这些配置对象add到里面,然后就可以使用类似getString()、getInteger()、getStringArray()、getList()等等方法来操作配置项了。

ConfigApp类的全部代码如下:

[java] view plain copy

  1. package org.semicloud.study.config;
  2. import java.util.Arrays;
  3. import org.apache.commons.configuration.CompositeConfiguration;
  4. import org.apache.commons.configuration.ConfigurationException;
  5. import org.apache.commons.configuration.HierarchicalINIConfiguration;
  6. import org.apache.commons.configuration.PropertiesConfiguration;
  7. import org.apache.commons.configuration.XMLConfiguration;
  8. import org.apache.commons.configuration.tree.xpath.XPathExpressionEngine;
  9. public class ConfigApp {
  10. public static void main(String[] args) throws ConfigurationException {
  11. readFromProperties();
  12. readFromIni();
  13. readFromXml();
  14. readFromCompositeConfig();
  15. }
  16. public static void readFromProperties() throws ConfigurationException {
  17. PropertiesConfiguration pcfg = new PropertiesConfiguration(“config/cfg.properties”);
  18. System.out.println(“1. read configuration from cfg.properties…”);
  19. System.out.println(“platform.jre = “ + pcfg.getString(“platform.jre”));
  20. System.out.println();
  21. }
  22. public static void readFromIni() throws ConfigurationException {
  23. HierarchicalINIConfiguration ini = new HierarchicalINIConfiguration(“config/cfg.ini”);
  24. System.out.println(“2. read configuration from cfg.ini…”);
  25. System.out.println(“os.edition = “ + ini.getString(“os.edition”));
  26. System.out.println();
  27. }
  28. public static void readFromXml() throws ConfigurationException {
  29. XMLConfiguration xml = new XMLConfiguration(“config/cfg.xml”);
  30. System.out.println(“3. read configuration from cfg.xml…”);
  31. // XML配置文件的简单使用(不使用XPath)
  32. System.out.println(“3.1 simple use..”);
  33. System.out.println(“teacher’s name = “ + xml.getString(“teacher.name”));
  34. System.out.println(“teacher’s age = “ + xml.getString(“teacher.age”));
  35. // 使用XPath查询配置文件
  36. System.out.println(“3.2 use xpath…”);
  37. xml.setExpressionEngine(new XPathExpressionEngine());
  38. // 查询姓名为foo的学生的算法课成绩
  39. String expr1 = “/student[@name=’foo’]/score[@course=’Algorithm’]“;
  40. System.out.println(“foo’s algorithm score = “ + xml.getString(expr1));
  41. // 查询所有学生的算法课成绩
  42. String expr2 = “/student//score[@course=’Algorithm’]“;
  43. System.out.println(“all students algorithm’s score = “ + Arrays.toString(xml.getStringArray(expr2)));
  44. // 查询姓名为foo的学生的性别
  45. String expr3 = “/student[@name=’foo’]/@gender”;
  46. System.out.println(“foo’s gender = “ + xml.getString(expr3));
  47. System.out.println();
  48. }
  49. public static void readFromCompositeConfig() throws ConfigurationException {
  50. System.out.println(“4. composite all configurations…”);
  51. CompositeConfiguration configurations = new CompositeConfiguration();
  52. HierarchicalINIConfiguration ini = new HierarchicalINIConfiguration(“config/cfg.ini”);
  53. PropertiesConfiguration props = new PropertiesConfiguration(“config/cfg.properties”);
  54. XMLConfiguration xml = new XMLConfiguration(“config/cfg.xml”);
  55. xml.setExpressionEngine(new XPathExpressionEngine());
  56. configurations.addConfiguration(ini);
  57. System.out.println(“add ini to composite configuration…”);
  58. configurations.addConfiguration(props);
  59. System.out.println(“add properties to composite configuration…”);
  60. configurations.addConfiguration(xml);
  61. System.out.println(“add xml to composite configuration…”);
  62. System.out.println(“platform.jre = “ + configurations.getString(“platform.jre”));
  63. System.out.println(“os.edition = “ + configurations.getString(“os.edition”));
  64. String expr1 = “/student[@name=’foo’]/score[@course=’Algorithm’]“;
  65. System.out.println(“foo’s algorithm score = “ + configurations.getString(expr1));
  66. }
  67. }

发表评论

表情:
评论列表 (有 0 条评论,511人围观)

还没有评论,来说两句吧...

相关阅读

    相关 configuration

    Apache commons-configuration 是一个开源组件,可以方便的对项目中的配置文件进行读取和保存,本文只说明如何使用该组件读取项目中的配置文件(以ini、p

    相关 linux configure

    configure是一个shell脚本,它可以自动设定源程序以符合各种不同平台上Unix系统的特性,并且根据系统叁数及环境产生合适的Makefile文件或是C的头文件(head