You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Get string valueStringappName = configuration.get("application.name");
// Get string with defaultStringencoding = configuration.get("default.file.encoding", "UTF-8");
// Get integer valueintport = configuration.getInt("server.port");
// Get integer with defaultintmaxConnections = configuration.getInt("database.connections.max", 10);
// Get boolean valuebooleandevMode = configuration.getBoolean("application.development");
// Get boolean with defaultbooleanreloadMode = configuration.getBoolean("default.reload.mode", false);
// Get double valuedoublethreshold = configuration.getDouble("performance.threshold");
// Get double with defaultdoublefactor = configuration.getDouble("scaling.factor", 1.5);
Setting Values
Method
Return Type
Description
set(String, String)
void
Set a string value
set(String, int)
void
Set an integer value
set(String, boolean)
void
Set a boolean value
set(String, double)
void
Set a double value
// Set string valueconfiguration.set("application.name", "MyApp");
// Set integer valueconfiguration.set("server.port", 8080);
// Set boolean valueconfiguration.set("application.development", true);
// Set double valueconfiguration.set("scaling.factor", 1.5);
Checking and Removing
Method
Return Type
Description
contains(String)
boolean
Check if a key exists
remove(String)
void
Remove a key-value pair
// Check if key existsif (configuration.contains("database.url")) {
// Use database URL
}
// Remove a keyconfiguration.remove("temporary.setting");
Loading Configuration
Method
Return Type
Description
load()
void
Load from default location
load(String)
void
Load from specified file path
load(URL)
void
Load from URL
// Load from default locationconfiguration.load();
// Load from specific fileconfiguration.load("config.properties");
// Load from URLconfiguration.load(newURL("http://config-server/app-config.properties"));
Collection Methods
Method
Return Type
Description
keySet()
Set
Get all configuration keys
getProperties()
Properties
Get configuration as Properties object
// Get all keysSet<String> keys = configuration.keySet();
for (Stringkey : keys) {
System.out.println(key + " = " + configuration.get(key));
}
// Get as PropertiesPropertiesprops = configuration.getProperties();
DefaultConfiguration
The DefaultConfiguration class is the standard implementation of the Configuration interface.
// Create a new configurationConfigurationconfiguration = newDefaultConfiguration();
// Load configurationconfiguration.load("config.properties");
// Use configurationStringappName = configuration.get("application.name");
// Override with system propertiesStringjavaHome = configuration.get("java.home");
if (javaHome == null) {
javaHome = System.getProperty("java.home");
}
// Set system property from configurationSystem.setProperty("app.name", configuration.get("application.name"));