Apache Commons:简单的使用Configuration读取和修改配置文件

1. 声明

当前内容主要为本人学习和使用Apache Commons Configuration来操作

当前内容参考:apache commons 官方文档

主要内容为:

  1. 读取和操作properties配置文件
  2. 读取和操作xml配置文件

基本pom依赖(configuration依赖beanutils)

<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-configuration2</artifactId>
	<version>2.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->
<dependency>
	<groupId>commons-beanutils</groupId>
	<artifactId>commons-beanutils</artifactId>
	<version>1.9.4</version>
</dependency>

2. 读取和操作properties配置文件demo

准备的db.properties文件

mysql.url=jdbc:mysql://localhost:3306/test
mysql.username=root
mysql.password=root
mysql.driverClassName=com.mysql.jdbc.Driver

demo

	public static void main(String[] args) {

		Configurations configs = new Configurations();
		loadAndModifyFromProperties(configs); // 加载和修改xml的配置文件
	}
	
	/**
	 * 
	 * @author hy
	 * @createTime 2021-06-20 14:47:12
	 * @description 实现配置文件的数据的获取和配置文件的更新
	 *
	 */
	private static void loadAndModifyFromProperties(Configurations configs) {
		// 默认直接读取类路径下的配置文件
		try {
			// 这个properties只是读取的操作(不能执行更新操作)
			// Configuration config = configs.properties(new File("db.properties"));
			// 采用这个builder方式就可以实现修改配置文件的操作了
			FileBasedConfigurationBuilder<PropertiesConfiguration> builder = configs.propertiesBuilder(new File("db.properties"));
			// FileBasedConfigurationBuilder<PropertiesConfiguration> builder = configs.fileBasedBuilder(PropertiesConfiguration.class, );
			Configuration config = builder.getConfiguration();
			// 输出元素
			printElements(config);
			// 更新配置文件
			config.setProperty("mysql.username", "abc");
			// config.addProperty("mysql.timeout", "10");
			// 保存配置文件
			builder.save();
		} catch (ConfigurationException cex) {
			// Something went wrong
			cex.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @author hy
	 * @createTime 2021-06-20 14:51:07
	 * @description 打印当前的元素
	 * @param config
	 *
	 */
	private static void printElements(Configuration config) {
		Iterator<String> keys = config.getKeys();
		while (keys.hasNext()) {
			String key = keys.next();
			String value = config.getString(key);
			System.out.println("key=" + key + ",value=" + value);
		}
	}

执行的结果:
在这里插入图片描述

在这里插入图片描述

3. 读取和修改xml配置文件

准备的db.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <dbs>
  	<db type="mysql">
  		<driverClassName>com.mysql.jdbc.Driver</driverClassName>
  		<username>root</username>
  		<password>root</password>
  		<url>jdbc:mysql://localhost:3306/test</url>
  	</db>
  	<db type="sqlserver">
  		<username>sa</username>
  		<password>111111</password>
  	</db>
  </dbs>
</configuration>

demo

public static void main(String[] args) {

		Configurations configs = new Configurations();
		loadAndModifyFromXml(configs); // 加载和修改xml的配置文件
	}
	
	/**
	 * 
	 * @author hy
	 * @createTime 2021-06-20 14:51:07
	 * @description 打印当前的元素
	 * @param config
	 *
	 */
	private static void printElements(Configuration config) {
		Iterator<String> keys = config.getKeys();
		while (keys.hasNext()) {
			String key = keys.next();
			String value = config.getString(key);
			System.out.println("key=" + key + ",value=" + value);
		}
	}
	
	private static void loadAndModifyFromXml(Configurations configs) {
		try {
			// 只能提供读取服务
			// Configuration config = configs.xml(new File("db.xml"));
			FileBasedConfigurationBuilder<XMLConfiguration> builder = configs.xmlBuilder(new File("db.xml"));
			Configuration config = builder.getConfiguration();
			printElements(config);
			// 开始修改当前的xml中的元素
			config.setProperty("dbs.db.username", "abc");
			config.addProperty("dbs.db.timeout", "50");
			builder.save();
		} catch (ConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

执行结果:(和前面一致,导致节点type=sqlserver的没有读取出来)

在这里插入图片描述
所以这里需要将当前的db标签变成sqlserver和mysql标签,这样就不会出现读取和写出的问题,保证properties的唯一性

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值