Performance: Environment variables in configuration
This affects the application startup time and configuration loading speed, impacting how fast the app becomes ready to serve requests.
public class AppConfig { @Value("${CONFIG_VALUE}") private String configValue; // Environment variable injected once at startup // No runtime file parsing needed }
public class AppConfig { @Value("${config.value}") private String configValue; // Using a heavy config file parsing at runtime public void loadConfig() { // Reads large config file on every request Properties props = new Properties(); try (InputStream input = new FileInputStream("config.properties")) { props.load(input); configValue = props.getProperty("config.value"); } catch (IOException e) { e.printStackTrace(); } } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Reading config files on every request | N/A | N/A | N/A | [X] Bad |
| Using environment variables injected at startup | N/A | N/A | N/A | [OK] Good |
DB_HOST in a Spring Boot application.properties file?${VAR_NAME} syntax to inject environment variables into configuration files.DB_HOST, the correct syntax is ${DB_HOST}.PORT in application.properties?${VAR_NAME:defaultValue} to provide a default if the environment variable is missing.server.port=${PORT:8080} uses ${PORT:8080}, which is the correct way to set default port 8080.application.properties snippet:app.name=${APP_NAME:MyApp}
app.port=${APP_PORT:9090}APP_NAME is set to TestApp and APP_PORT is not set, what will be the values of app.name and app.port at runtime?app.name uses this value, ignoring the default.app.port uses the default value 9090.application.properties line:database.url=jdbc:mysql://${DB_HOST}:${DB_PORT:3306}/mydbDB_HOST is not set. What is the best fix?${DB_HOST:localhost} to supply a default host if the environment variable is missing.application.properties line correctly uses environment variables REDIS_HOST and REDIS_PORT with defaults localhost and 6379 respectively?${VAR_NAME:default} to provide defaults for environment variables.${REDIS_HOST:localhost} and ${REDIS_PORT:6379} with colon syntax and includes the colon separator between host and port.Pass the test and this pattern moves unknown