Sobes.tech
Junior — Middle

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:

  1. Properties files (.properties) — a classic method where configuration is stored in a text format key=value. Loaded using Properties.

  2. YAML or JSON files — more structured formats, often used with libraries like Jackson or SnakeYAML.

  3. Environment variables — convenient for container and cloud configurations.

  4. Command-line arguments — passed during application startup.

  5. Spring Framework — uses application.properties or application.yml, supports profiles, external configurations, and automatic binding to POJOs.

  6. 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.