commons-configuration 使用实例

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 [email protected]
 *
 */
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"));

	}
}


你可能感兴趣的:(使用实例)