Discover how a simple environment variable can save hours of debugging and keep your secrets safe!
Why Environment variables in configuration in Spring Boot? - Purpose & Use Cases
Imagine you have a Spring Boot app that needs different settings for your laptop, your colleague's computer, and the production server. You try to change the database URL, passwords, and ports by editing the code every time.
Manually changing configuration in code is risky and slow. You might forget to change something, accidentally commit secrets to version control, or cause bugs by mixing settings. It's hard to keep track and update across many environments.
Using environment variables lets you keep configuration outside your code. Spring Boot reads these variables automatically, so you can switch settings by just changing environment values without touching the code.
String dbUrl = "jdbc:mysql://localhost:3306/devdb"; // hardcoded in code
spring.datasource.url=${DB_URL} # reads from environment variableThis makes your app flexible and secure, allowing easy changes to settings per environment without code changes or risks.
When deploying your app to the cloud, you set environment variables for database credentials and API keys on the server. Your app picks them up automatically, so you never expose secrets in your code.
Hardcoding config causes errors and security risks.
Environment variables separate config from code safely.
Spring Boot reads these variables automatically for easy setup.
Practice
DB_HOST in a Spring Boot application.properties file?Solution
Step 1: Understand Spring Boot environment variable syntax
Spring Boot uses${VAR_NAME}syntax to inject environment variables into configuration files.Step 2: Match the syntax to the variable name
For the variableDB_HOST, the correct syntax is${DB_HOST}.Final Answer:
Use ${DB_HOST} to reference the environment variable. -> Option BQuick Check:
Environment variable syntax = ${VAR_NAME} [OK]
- Using #VAR_NAME# instead of ${VAR_NAME}
- Trying to call env(VAR_NAME) like a function
- Using variable name without any syntax
PORT in application.properties?Solution
Step 1: Recall default value syntax in Spring Boot
Spring Boot uses${VAR_NAME:defaultValue}to provide a default if the environment variable is missing.Step 2: Identify the correct syntax among options
server.port=${PORT:8080}uses${PORT:8080}, which is the correct way to set default port 8080.Final Answer:
server.port=${PORT:8080} -> Option AQuick Check:
Default value syntax = ${VAR_NAME:default} [OK]
- Using = instead of : for default values
- Using shell-style ${VAR:-default} which is invalid here
- Using $VAR without braces or default
application.properties snippet:app.name=${APP_NAME:MyApp}
app.port=${APP_PORT:9090}If the environment variables
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?Solution
Step 1: Check environment variable APP_NAME
APP_NAME is set to 'TestApp', soapp.nameuses this value, ignoring the default.Step 2: Check environment variable APP_PORT
APP_PORT is not set, soapp.portuses the default value 9090.Final Answer:
app.name=TestApp and app.port=9090 -> Option AQuick Check:
Set var overrides default; unset uses default [OK]
- Assuming default is used even if env var is set
- Assuming null if env var is missing without default
- Confusing variable names or values
application.properties line:database.url=jdbc:mysql://${DB_HOST}:${DB_PORT:3306}/mydbWhen running the app, it throws an error because
DB_HOST is not set. What is the best fix?Solution
Step 1: Identify cause of error
DB_HOST is not set and no default is provided, causing Spring Boot to fail resolving the variable.Step 2: Provide a default value using correct syntax
Use${DB_HOST:localhost}to supply a default host if the environment variable is missing.Final Answer:
Set a default value for DB_HOST like ${DB_HOST:localhost}. -> Option CQuick Check:
Missing env var needs default to avoid errors [OK]
- Using shell syntax ${VAR:-default} which Spring Boot does not support
- Assuming Spring Boot auto-fills missing env vars
- Hardcoding values reduces flexibility
application.properties line correctly uses environment variables REDIS_HOST and REDIS_PORT with defaults localhost and 6379 respectively?Solution
Step 1: Recall Spring Boot default value syntax
Spring Boot uses${VAR_NAME:default}to provide defaults for environment variables.Step 2: Check each option for correct syntax and completeness
redis.url=redis://${REDIS_HOST:localhost}:${REDIS_PORT:6379} correctly uses${REDIS_HOST:localhost}and${REDIS_PORT:6379}with colon syntax and includes the colon separator between host and port.Final Answer:
redis.url=redis://${REDIS_HOST:localhost}:${REDIS_PORT:6379} -> Option DQuick Check:
Use colon : for defaults; include separators explicitly [OK]
- Using shell-style :- instead of : for defaults
- Omitting colon between host and port
- Using $VAR without braces or defaults
Ready to prove it?
Pass the test and this pattern moves unknown
