Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Combining Properties Files, System Properties, and Environment Variables | System Properties and Environment Variables
Mastering Java Application Configuration

bookCombining Properties Files, System Properties, and Environment Variables

When you build Java applications, you often need to configure them for different environments, such as development, testing, and production. Relying on a single source for configuration, like a properties file, can be limiting. By combining properties files, system properties, and environment variables, you can make your applications more flexible and easier to manage.

A common approach is to establish a precedence hierarchy among these sources. Typically, you start with values from a properties file as defaults. Then, you check for system properties, which can override the defaults when the application is started with -Dproperty=value flags. Finally, environment variables—set outside the JVM—can provide the highest level of override, allowing for last-minute changes without modifying files or command-line arguments.

A recommended precedence order is:

  1. Properties file: provides default values;
  2. System properties: override properties file values if present;
  3. Environment variables: override both system properties and properties file values.

This hierarchy gives you maximum flexibility and control. For example, you can define sensible defaults in your properties file, let testers or deployers set system properties for specific runs, and allow administrators to override any setting at the environment level for production deployments.

ConfigLoader.java

ConfigLoader.java

copy
123456789101112131415161718192021222324252627282930313233343536373839
package com.example; import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; public class ConfigLoader { public static void main(String[] args) throws IOException { Properties props = new Properties(); // Load properties from file try (FileInputStream fis = new FileInputStream("app.properties")) { props.load(fis); } // Keys to load String[] keys = {"app.name", "app.port", "app.mode"}; for (String key : keys) { String value = getConfigValue(key, props); System.out.println(key + " = " + value); } } // Lookup order: environment variable > system property > properties file private static String getConfigValue(String key, Properties props) { // Convert property key to environment variable style (e.g., APP_NAME) String envKey = key.toUpperCase().replace('.', '_'); String envValue = System.getenv(envKey); if (envValue != null) { return envValue; } String sysPropValue = System.getProperty(key); if (sysPropValue != null) { return sysPropValue; } return props.getProperty(key); } }

To keep your configuration flexible and understandable, always document the sources and precedence of each configuration value. Use comments in your properties files to explain defaults. In your application's documentation, clearly state which environment variables and system properties are recognized, and how they interact. This helps users and administrators know where to set values and reduces confusion during deployment or troubleshooting.

Managing configuration sources is easier when you:

  • Use consistent naming conventions across files, properties, and environment variables;
  • Avoid hard-coding sensitive values directly in source code or properties files;
  • Provide sample properties files and clear instructions for overriding values via system properties or environment variables;
  • Regularly review and update documentation as configuration options evolve.

A well-documented configuration strategy makes your applications easier to support and adapt to new requirements.

ConfigLookup.java

ConfigLookup.java

copy
123456789101112131415161718192021222324252627282930313233343536
package com.example; import java.util.Properties; public class ConfigLookup { private final Properties props; public ConfigLookup(Properties props) { this.props = props; } public String get(String key) { String envKey = key.toUpperCase().replace('.', '_'); String envValue = System.getenv(envKey); if (envValue != null) { return envValue; } String sysPropValue = System.getProperty(key); if (sysPropValue != null) { return sysPropValue; } return props.getProperty(key); } public static void main(String[] args) { Properties defaults = new Properties(); defaults.setProperty("db.url", "jdbc:mysql://localhost:3306/mydb"); defaults.setProperty("db.user", "root"); ConfigLookup config = new ConfigLookup(defaults); System.out.println("db.url = " + config.get("db.url")); System.out.println("db.user = " + config.get("db.user")); } }

1. What is a common strategy for resolving conflicts between configuration sources?

2. Why might you want to allow environment variables to override properties file values?

question mark

What is a common strategy for resolving conflicts between configuration sources?

Select the correct answer

question mark

Why might you want to allow environment variables to override properties file values?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 3

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

bookCombining Properties Files, System Properties, and Environment Variables

Glissez pour afficher le menu

When you build Java applications, you often need to configure them for different environments, such as development, testing, and production. Relying on a single source for configuration, like a properties file, can be limiting. By combining properties files, system properties, and environment variables, you can make your applications more flexible and easier to manage.

A common approach is to establish a precedence hierarchy among these sources. Typically, you start with values from a properties file as defaults. Then, you check for system properties, which can override the defaults when the application is started with -Dproperty=value flags. Finally, environment variables—set outside the JVM—can provide the highest level of override, allowing for last-minute changes without modifying files or command-line arguments.

A recommended precedence order is:

  1. Properties file: provides default values;
  2. System properties: override properties file values if present;
  3. Environment variables: override both system properties and properties file values.

This hierarchy gives you maximum flexibility and control. For example, you can define sensible defaults in your properties file, let testers or deployers set system properties for specific runs, and allow administrators to override any setting at the environment level for production deployments.

ConfigLoader.java

ConfigLoader.java

copy
123456789101112131415161718192021222324252627282930313233343536373839
package com.example; import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; public class ConfigLoader { public static void main(String[] args) throws IOException { Properties props = new Properties(); // Load properties from file try (FileInputStream fis = new FileInputStream("app.properties")) { props.load(fis); } // Keys to load String[] keys = {"app.name", "app.port", "app.mode"}; for (String key : keys) { String value = getConfigValue(key, props); System.out.println(key + " = " + value); } } // Lookup order: environment variable > system property > properties file private static String getConfigValue(String key, Properties props) { // Convert property key to environment variable style (e.g., APP_NAME) String envKey = key.toUpperCase().replace('.', '_'); String envValue = System.getenv(envKey); if (envValue != null) { return envValue; } String sysPropValue = System.getProperty(key); if (sysPropValue != null) { return sysPropValue; } return props.getProperty(key); } }

To keep your configuration flexible and understandable, always document the sources and precedence of each configuration value. Use comments in your properties files to explain defaults. In your application's documentation, clearly state which environment variables and system properties are recognized, and how they interact. This helps users and administrators know where to set values and reduces confusion during deployment or troubleshooting.

Managing configuration sources is easier when you:

  • Use consistent naming conventions across files, properties, and environment variables;
  • Avoid hard-coding sensitive values directly in source code or properties files;
  • Provide sample properties files and clear instructions for overriding values via system properties or environment variables;
  • Regularly review and update documentation as configuration options evolve.

A well-documented configuration strategy makes your applications easier to support and adapt to new requirements.

ConfigLookup.java

ConfigLookup.java

copy
123456789101112131415161718192021222324252627282930313233343536
package com.example; import java.util.Properties; public class ConfigLookup { private final Properties props; public ConfigLookup(Properties props) { this.props = props; } public String get(String key) { String envKey = key.toUpperCase().replace('.', '_'); String envValue = System.getenv(envKey); if (envValue != null) { return envValue; } String sysPropValue = System.getProperty(key); if (sysPropValue != null) { return sysPropValue; } return props.getProperty(key); } public static void main(String[] args) { Properties defaults = new Properties(); defaults.setProperty("db.url", "jdbc:mysql://localhost:3306/mydb"); defaults.setProperty("db.user", "root"); ConfigLookup config = new ConfigLookup(defaults); System.out.println("db.url = " + config.get("db.url")); System.out.println("db.user = " + config.get("db.user")); } }

1. What is a common strategy for resolving conflicts between configuration sources?

2. Why might you want to allow environment variables to override properties file values?

question mark

What is a common strategy for resolving conflicts between configuration sources?

Select the correct answer

question mark

Why might you want to allow environment variables to override properties file values?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 3
some-alt