Environment variables let you set values outside your code. This helps keep your app flexible and secure.
Environment variables in configuration in Spring Boot
spring.datasource.url=${DB_URL}
spring.datasource.username=${DB_USER}
spring.datasource.password=${DB_PASS}Use ${VARIABLE_NAME} to refer to environment variables in application.properties or application.yml.
If the environment variable is missing, Spring Boot may fail to start or use a default if provided.
spring.datasource.url=${DB_URL}
spring.datasource.username=${DB_USER}
spring.datasource.password=${DB_PASS}:8080 to provide a default value if PORT is not set.server.port=${PORT:8080}app.name=${APP_NAME:MyApp}This Spring Boot app reads an environment variable APP_GREETING to show a greeting message. If the variable is not set, it shows 'Hello, World!'.
Run the app and visit /greet to see the message.
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } @RestController class ConfigController { @Value("${APP_GREETING:Hello, World!}") private String greeting; @GetMapping("/greet") public String greet() { return greeting; } }
Environment variables are case-sensitive on most systems.
Use application.yml for cleaner configuration if preferred.
Remember to set environment variables before starting your Spring Boot app.
Environment variables let you configure apps without changing code.
Use ${VAR_NAME} syntax in Spring Boot config files.
You can provide default values with : after the variable name.
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
