0
0
Spring Bootframework~10 mins

@Value for property injection 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 inject the property value using @Value annotation.

Spring Boot
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class MyComponent {
    @Value("[1]")
    private String appName;
}
Drag options to blanks, or click blank then click option'
A"appName"
B"app.name"
C"${appName}"
D"${app.name}"
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to use ${} around the property key.
Using the variable name instead of the property key.
2fill in blank
medium

Complete the code to inject a default value if the property is missing.

Spring Boot
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Config {
    @Value("[1]")
    private String serverPort;
}
Drag options to blanks, or click blank then click option'
A"server.port:8080"
B"${server.port}"
C"${server.port:8080}"
D"server.port"
Attempts:
3 left
💡 Hint
Common Mistakes
Not including the default value syntax with colon.
Using quotes incorrectly around the property key.
3fill in blank
hard

Fix the error in the code to correctly inject an integer property value.

Spring Boot
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class ServerConfig {
    @Value("[1]")
    private int maxConnections;
}
Drag options to blanks, or click blank then click option'
A"${server.maxConnections}"
B"${server.maxConnections:100}"
C"server.maxConnections"
D"server.maxConnections:100"
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the ${} syntax.
Placing default value outside the ${}.
4fill in blank
hard

Fill both blanks to inject a list of strings from a comma-separated property.

Spring Boot
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.List;

@Component
public class ListConfig {
    @Value("[1]")
    private List<String> servers;

    public List<String> getServers() {
        return servers;
    }
}
Drag options to blanks, or click blank then click option'
A"${servers.list}"
B"${servers.list:}"
C"${servers.list.split(',')}"
D"${servers.list:localhost,127.0.0.1}"
Attempts:
3 left
💡 Hint
Common Mistakes
Not splitting the string to a list.
Missing default value for the property.
5fill in blank
hard

Fill all three blanks to inject a nested property with default and convert it to uppercase.

Spring Boot
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class NestedConfig {
    @Value("[1]")
    private String env;

    public String getEnvUpper() {
        return env.[2]().[3]();
    }
}
Drag options to blanks, or click blank then click option'
A"${environment.name:dev}"
BtoLowerCase
CtoUpperCase
Dtrim
Attempts:
3 left
💡 Hint
Common Mistakes
Not using default value in @Value.
Calling methods in wrong order.