What methods are used to store external configuration data in applications?
sobes.tech AI
Answer from AI
In Java applications, external configuration data are usually stored and loaded using the following methods:
-
Properties files (.properties) — a classic method where configuration is stored in a text format key=value. Loaded using
Properties. -
YAML or JSON files — more structured formats, often used with libraries like Jackson or SnakeYAML.
-
Environment variables — convenient for container and cloud configurations.
-
Command-line arguments — passed during application startup.
-
Spring Framework — uses application.properties or application.yml, supports profiles, external configurations, and automatic binding to POJOs.
-
Configuration servers — e.g., Spring Cloud Config, for centralized management.
Example of loading from a properties file:
Properties props = new Properties();
try (InputStream input = new FileInputStream("config.properties")) {
props.load(input);
String url = props.getProperty("db.url");
} catch (IOException ex) {
ex.printStackTrace();
}
The choice of method depends on the application's requirements for structure, dynamism, and environment.