📖 Scenario: You are building a Spring Boot application that needs to use environment variables to configure its behavior. This is common when deploying apps to different environments like development, testing, and production.
🎯 Goal: Create a Spring Boot application that reads a custom environment variable and uses it in the configuration to display a greeting message.
📋 What You'll Learn
Create a property in application.properties that reads an environment variable
Create a Spring Boot @Component class that uses @Value to inject the property
Write a method that returns the greeting message using the injected property
Add a @RestController with a GET endpoint to return the greeting message
💡 Why This Matters
🌍 Real World
Using environment variables for configuration is essential for deploying Spring Boot applications across different environments without changing code.
💼 Career
Many companies use environment variables to manage secrets, API keys, and environment-specific settings in their Spring Boot microservices.
Progress0 / 4 steps
1
Create application.properties with environment variable placeholder
Create a file named application.properties and add a property called app.greeting that reads the environment variable GREETING_MESSAGE with a default value of Hello, World!.
Spring Boot
Hint
Use the syntax property=${ENV_VAR:defaultValue} to read environment variables in Spring Boot.
2
Create GreetingService component with @Value injection
Create a class named GreetingService annotated with @Component. Inject the property app.greeting into a private String field called greeting using @Value. Add a public method getGreeting() that returns the greeting string.
Spring Boot
Hint
Use @Value("${app.greeting}") to inject the property into the field.
3
Create GreetingController with GET endpoint
Create a class named GreetingController annotated with @RestController. Inject GreetingService via constructor. Add a GET mapping method greet() mapped to /greet that returns the greeting string from GreetingService.getGreeting().
Spring Boot
Hint
Use constructor injection for GreetingService and @GetMapping("/greet") for the endpoint.
4
Add main Spring Boot application class
Create a class named DemoApplication annotated with @SpringBootApplication. Add the main method that runs SpringApplication.run(DemoApplication.class, args).
Spring Boot
Hint
The main class must have @SpringBootApplication and a main method to start the app.
Practice
(1/5)
1. What is the correct way to reference an environment variable named DB_HOST in a Spring Boot application.properties file?
easy
A. Use #DB_HOST# to reference the environment variable.
B. Use ${DB_HOST} to reference the environment variable.
C. Use env(DB_HOST) to reference the environment variable.
D. Use DB_HOST directly without any syntax.
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 variable DB_HOST, the correct syntax is ${DB_HOST}.
Final Answer:
Use ${DB_HOST} to reference the environment variable. -> Option B
Quick Check:
Environment variable syntax = ${VAR_NAME} [OK]
Hint: Remember: Spring Boot uses ${VAR_NAME} for env variables [OK]
Common Mistakes:
Using #VAR_NAME# instead of ${VAR_NAME}
Trying to call env(VAR_NAME) like a function
Using variable name without any syntax
2. Which of the following is the correct syntax to provide a default value for an environment variable PORT in application.properties?
easy
A. server.port=${PORT:8080}
B. server.port=${PORT:-8080}
C. server.port=${PORT=8080}
D. server.port=$PORT?8080
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 A
Quick Check:
Default value syntax = ${VAR_NAME:default} [OK]
Hint: Use colon : after variable name for default values [OK]
Common Mistakes:
Using = instead of : for default values
Using shell-style ${VAR:-default} which is invalid here
Using $VAR without braces or default
3. Given the following application.properties snippet:
When running the app, it throws an error because DB_HOST is not set. What is the best fix?
medium
A. Ignore the error; Spring Boot will use a fallback automatically.
B. Remove ${DB_HOST} and hardcode the host.
C. Set a default value for DB_HOST like ${DB_HOST:localhost}.
D. Change syntax to ${DB_HOST:-localhost} to fix it.
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 C
Quick Check:
Missing env var needs default to avoid errors [OK]
Hint: Always provide defaults for required env vars to avoid errors [OK]
Common Mistakes:
Using shell syntax ${VAR:-default} which Spring Boot does not support
Assuming Spring Boot auto-fills missing env vars
Hardcoding values reduces flexibility
5. You want to configure a Spring Boot app to read a nested environment variable for a Redis URL with fallback defaults. Which application.properties line correctly uses environment variables REDIS_HOST and REDIS_PORT with defaults localhost and 6379 respectively?
hard
A. redis.url=redis://$REDIS_HOST:$REDIS_PORT
B. redis.url=redis://${REDIS_HOST:-localhost}:${REDIS_PORT:-6379}
C. redis.url=redis://${REDIS_HOST}${REDIS_PORT}
D. redis.url=redis://${REDIS_HOST:localhost}:${REDIS_PORT:6379}
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 D
Quick Check:
Use colon : for defaults; include separators explicitly [OK]
Hint: Use ${VAR:default} and include separators like : explicitly [OK]
Common Mistakes:
Using shell-style :- instead of : for defaults
Omitting colon between host and port
Using $VAR without braces or defaults
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.