0
0
Spring Bootframework~10 mins

Environment variables in configuration in Spring Boot - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to read an environment variable in Spring Boot application.properties.

Spring Boot
server.port=[1]
Drag options to blanks, or click blank then click option'
A${PORT}
BPORT
C$PORT
D${PORT:8080}
Attempts:
3 left
💡 Hint
Common Mistakes
Using $PORT without braces
Not providing default value
Using plain PORT without ${}
2fill in blank
medium

Complete the annotation to inject an environment variable into a Spring Boot component.

Spring Boot
@Value("[1]")
private String dbUrl;
Drag options to blanks, or click blank then click option'
A"${DATABASE_URL:jdbc:h2:mem:testdb}"
B"DATABASE_URL"
C"$DATABASE_URL"
D"${DATABASE_URL}"
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting ${} syntax
Not adding quotes around the string
Using $ without braces
3fill in blank
hard

Fix the error in this Spring Boot configuration to correctly use environment variables with default values.

Spring Boot
spring.datasource.url=[1]
Drag options to blanks, or click blank then click option'
ADB_URL:jdbc:mysql://localhost:3306/mydb
B$DB_URL:jdbc:mysql://localhost:3306/mydb
C${DB_URL:jdbc:mysql://localhost:3306/mydb}
D${DB_URL, jdbc:mysql://localhost:3306/mydb}
Attempts:
3 left
💡 Hint
Common Mistakes
Using $ without braces
Using comma instead of colon
Omitting ${} syntax
4fill in blank
hard

Fill both blanks to define a Spring Boot property that reads an environment variable with a fallback and inject it using @Value.

Spring Boot
@Value("[1]")
private String apiKey;

# application.properties
external.api.key=[2]
Drag options to blanks, or click blank then click option'
A"${API_KEY:defaultKey}"
B${API_KEY:defaultKey}
C"${EXTERNAL_API_KEY:defaultKey}"
D${EXTERNAL_API_KEY:defaultKey}
Attempts:
3 left
💡 Hint
Common Mistakes
Missing quotes in @Value
Adding quotes in properties file
Using wrong variable names
5fill in blank
hard

Fill all three blanks to create a configuration class that reads environment variables with defaults using @Value and exposes them as beans.

Spring Boot
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {

    @Value("[1]")
    private String host;

    @Value("[2]")
    private int port;

    @Bean
    public String serviceUrl() {
        return "http://" + host + ":" + [3];
    }
}
Drag options to blanks, or click blank then click option'
A${SERVICE_HOST:localhost}
B${SERVICE_PORT:8080}
Cport
Dhost
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes incorrectly
Using variable names instead of ${} in @Value
Using host instead of port in return