commons-configuration 使用实例

原创
2014/12/15 15:55
阅读数 337
import java.io.FileReader;
import java.net.URL;

import org.apache.commons.configuration.BaseConfiguration;
import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.EnvironmentConfiguration;
import org.apache.commons.configuration.FileConfiguration;
import org.apache.commons.configuration.INIConfiguration;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.XMLConfiguration;

/**
 * commons-configuration 使用实例
 * 
 * @author 542335496@qq.com
 *
 */
public class testMain {

	public static void main(String[] args) throws Exception {

		BaseConfiguration config = new BaseConfiguration();
		config.setThrowExceptionOnMissing(true);
		config.setProperty("number", "11");

		System.out.println(config.getString("number"));

		CompositeConfiguration compositeConfiguration = new CompositeConfiguration();

		compositeConfiguration.addConfiguration(new PropertiesConfiguration(
				"test.properties"));
		System.out.println(compositeConfiguration.getString("test.unescape"));
		Object object = compositeConfiguration.getProperty("include");
		System.out.println(object);

		Configuration configuration = new PropertiesConfiguration(
				"testClasspath.properties");
		System.out.println(configuration.getProperty("packages"));
		XMLConfiguration xmlConfiguration = new XMLConfiguration("sample.xml");
		xmlConfiguration.load();
		System.out.println(xmlConfiguration.getString("Employee.Name"));
		System.out
				.println(xmlConfiguration.getString("Employee.user.username"));

		EnvironmentConfiguration environmentConfiguration = new EnvironmentConfiguration();
		System.out.println(environmentConfiguration.getString("classpath"));
		System.out.println(environmentConfiguration.getString("user.home"));

		FileConfiguration fileConfiguration = new PropertiesConfiguration();
		fileConfiguration
				.setURL(new URL(
						"https://repo1.maven.org/maven2/io/fastjson/qbit/0.29/qbit-0.29.pom"));

		System.out.println(fileConfiguration.getBasePath());
		System.out.println(fileConfiguration.getFileName());
		System.out.println(fileConfiguration.getString("modelVersion"));

		INIConfiguration instance = new INIConfiguration();
		instance.load(new FileReader("c:/Windows/msdfmap.ini"));
		// instance.save();
		System.out.println(instance.getString("sql AuthorById.Sql"));

	}
}


展开阅读全文
打赏
0
1 收藏
分享
加载中
更多评论
打赏
0 评论
1 收藏
0
分享
返回顶部
顶部