The Commons Configuration software library provides a generic configuration interface which enables a Java application to read configuration data from a variety of sources.
database.properties
database.host = db.acme.com
database.port = 8199
database.user = admin
database.password = ???
database.timeout = 60000
database.type = oracle,sqlServer,mySql
public class MyConfiguration {
public static void main(String[] args) throws ConfigurationException {
String path = MyConfiguration.class.getClassLoader().getResource("database.properties").getPath();
PropertiesConfiguration config = new PropertiesConfiguration(path);
String dbHost = config.getString("database.host");
int dbPort = config.getInt("database.port");
String dbUser = config.getString("database.user");
String dbPassword = config.getString("database.password", "secret");
Long dbTimeout = config.getLong("database.timeout");
String dbType = config.getString("database.type");
System.out.println(dbHost);
System.out.println(dbPort);
System.out.println(dbUser);
System.out.println(dbPassword);
System.out.println(dbTimeout);
System.out.println(dbType);
}
}
result:
db.acme.com
8199
admin
???
60000
oracle
Process finished with exit code 0
dbType print is “oracle” not “oracle,sqlServer,mySql”
Configuration 默认逗号为分隔符 源码如下:
设置分隔符关闭即可打印出”oracle,sqlServer,mySql”
config.setListDelimiter((char) 0);
//config.setDelimiterParsingDisabled(true);
config.load(path);
两种都可以打印出来
Automatic Reloading
config.setReloadingStrategy(new FileChangedReloadingStrategy());