0
0
Spring Bootframework~20 mins

@Value for property injection in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Property Injection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Spring Boot component using @Value?
Consider this Spring Boot component:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Greeting {
    @Value("${app.greeting}")
    private String message;

    public String getMessage() {
        return message;
    }
}

If the application.properties contains app.greeting=Hello, Spring!, what will getMessage() return?
Anull
B${app.greeting}
CCompilation error due to missing setter
DHello, Spring!
Attempts:
2 left
💡 Hint
Think about how @Value injects property values into fields.
📝 Syntax
intermediate
2:00remaining
Which option correctly injects a default value with @Value?
You want to inject a property app.name but provide a default value "MyApp" if the property is missing. Which @Value syntax is correct?
A@Value("${app.name:MyApp}")
B@Value("${app.name|MyApp}")
C@Value("${app.name?MyApp}")
D@Value("${app.name?default=MyApp}")
Attempts:
2 left
💡 Hint
Spring uses colon ':' to specify default values in @Value expressions.
🔧 Debug
advanced
2:00remaining
Why does this @Value injection fail with a NullPointerException?
Given this code:
@Component
public class Config {
    @Value("${app.timeout}")
    private Integer timeout;

    public int getTimeout() {
        return timeout;
    }
}

and app.timeout is not set in properties, what causes a NullPointerException when calling getTimeout()?
AMissing @Autowired causes field not to be injected
BSpring cannot inject Integer fields, must use int primitive
Ctimeout is null because property is missing, unboxing to int causes NullPointerException
DThe @Value annotation syntax is invalid for Integer fields
Attempts:
2 left
💡 Hint
Consider what happens when a null Integer is converted to int.
🧠 Conceptual
advanced
2:00remaining
What happens if you use @Value on a static field?
Consider this code snippet:
@Component
public class StaticConfig {
    @Value("${app.version}")
    private static String version;

    public static String getVersion() {
        return version;
    }
}

What will be the behavior of the version field?
Aversion is injected but only after calling getVersion() once
Bversion remains null because Spring does not inject @Value into static fields
CCompilation error because @Value cannot be used on static fields
Dversion is injected correctly with the property value
Attempts:
2 left
💡 Hint
Think about how Spring manages instance fields versus static fields.
state_output
expert
2:00remaining
What is the output of this Spring Boot component with SpEL in @Value?
Given this component:
@Component
public class Calculator {
    @Value("#{2 * 3 + 4}")
    private int result;

    public int getResult() {
        return result;
    }
}

What will getResult() return?
A10
B14
C6
DRuntime error due to invalid expression
Attempts:
2 left
💡 Hint
Evaluate the expression 2 * 3 + 4 using normal math rules.