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?✗ Incorrect
The correct syntax uses
${property.name} inside quotes to inject the property value.How do you provide a default value of 8080 for
server.port if it is missing?✗ Incorrect
The colon
: syntax inside ${} provides a default value.Where can you NOT use
@Value in a Spring bean?✗ Incorrect
@Value cannot be used on local variables inside methods because Spring injects values during bean creation.What will happen if a property used in
@Value is missing and no default is set?✗ Incorrect
Spring fails to start because it cannot resolve the required property.
Which of these is a valid example of injecting a property with
@Value?✗ Incorrect
Only option C uses the correct syntax with
${} and quotes.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.