0
0
Spring Bootframework~5 mins

@Value for property injection in Spring Boot - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the @Value annotation in Spring Boot?
The @Value annotation is used to inject values from property files or environment variables directly into Spring-managed beans.
Click to reveal answer
beginner
How do you inject a property named app.name from application.properties using @Value?
You use @Value("${app.name}") on a field or setter method to inject the value of app.name from the properties file.
Click to reveal answer
intermediate
Can @Value inject default values if the property is missing? How?
Yes. You can provide a default value using the syntax @Value("${property.name:defaultValue}"). If property.name is missing, defaultValue is used.
Click to reveal answer
beginner
Where can @Value be applied in a Spring bean?
@Value can be applied on fields, constructor parameters, or setter methods to inject property values.
Click to reveal answer
intermediate
What happens if you use @Value with a property that does not exist and no default value is provided?
Spring will throw an exception during application startup because it cannot resolve the property value.
Click to reveal answer
Which syntax correctly injects the property server.port using @Value?
A@Value("#server.port")
B@Value("server.port")
C@Value(server.port)
D@Value("${server.port}")
How do you provide a default value of 8080 for server.port if it is missing?
A@Value("${server.port:8080}")
B@Value("${server.port?8080}")
C@Value("${server.port|8080}")
D@Value("${server.port=8080}")
Where can you NOT use @Value in a Spring bean?
AOn local variables inside methods
BOn constructor parameters
COn setter methods
DOn private fields
What will happen if a property used in @Value is missing and no default is set?
ASpring uses null as the value
BSpring ignores the missing property
CSpring throws an exception at startup
DSpring logs a warning but continues
Which of these is a valid example of injecting a property with @Value?
A@Value("my.property") private String prop;
B@Value("${my.property}") private String prop;
C@Value(my.property) private String prop;
D@Value(#my.property) private String prop;
Explain how to use @Value to inject a property from application.properties into a Spring bean field.
Think about how Spring reads properties and injects them.
You got /4 concepts.
    Describe what happens if a property referenced by @Value is missing and no default value is provided.
    Consider Spring's behavior when required config is missing.
    You got /4 concepts.