加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用common-configuration进行对配置文件的操作(xml,properties

发布时间:2020-12-16 08:58:27 所属栏目:百科 来源:网络整理
导读:apache提供了 commons-c onfiguration-1.0.jar进行配置文件的操作。 可以在官网上下在common-configruation的jar包。 官方介绍: The Commons Configuration software library provides a generic configuration interface which enables a Java application

apache提供了commons-configuration-1.0.jar进行配置文件的操作。

可以在官网上下在common-configruation的jar包。

官方介绍:

The Commons Configuration software library provides a generic configuration interface which enables a Java application to read configuration data from a variety of sources. Commons Configuration provides typed access to single,and multi-valued configuration parameters as demonstrated by the following code:


Double double = config.getDouble("number");
Integer integer getInteger

Configuration parameters may be loaded from the following sources:

  • Properties files
  • XML documents
  • Windows INI files
  • Property list files (plist)
  • JNDI
  • JDBC Datasource
  • System properties
  • Applet parameters
  • Servlet parameters
测试代码:

<?xml version="1.0" encoding="UTF-8"?>
<pserson>
    <username>
        <zhangsan>
            11100141
        </zhangsan>
        <lisi>
            11100142
        </lisi>
    </username>

    <password>123456</password>
    <sex>male</sex>
    <address>beijingshichaoyangqu</address>
</pserson>

public class CommonConfigurationTest {

    public static void main(String[] args) {

        String resources = "commonConfigruationXML.xml";
        try {
            Configuration xml = new XMLConfiguration(resources);
            List list =xml.getList("username.zhangsan");
            System.out.println(list);

        } catch (ConfigurationException e) {
            e.printStackTrace();
        }


    }
}

对属性文件进行的操作:

dbcp.username=root
dbcp.password=root
driver=com.mysql.jdbc.Driver

package com.jd.list.utils;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;

/**
 * Created by guojiangjiang on 2015/8/24.
 */
public class CommonConfigurationTest {

    public static void main(String[] args) {

        String resources = "commonConfigurationprop.properties";
        try {
            Configuration prop = new PropertiesConfiguration(resources);
            Object object =prop.getProperty("driver");
            System.out.println(object);

        } catch (ConfigurationException e) {
            e.printStackTrace();
        }

    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读