Bird
0
0

How should you correctly inject the integer property server.port from application.properties into a Spring bean field using @Value?

easy📝 Syntax Q3 of 15
Spring Boot - Spring Annotations
How should you correctly inject the integer property server.port from application.properties into a Spring bean field using @Value?
A@Value("${server.port}") private int port;
B@Value(server.port) private int port;
C@Value("server.port") private int port;
D@Value(${server.port}) private int port;
Step-by-Step Solution
Solution:
  1. Step 1: Use correct syntax for @Value

    The property placeholder must be enclosed in quotes and use the ${} syntax: @Value("${property.name}").
  2. Step 2: Check for common mistakes

    Options B, C, and D miss quotes or the ${} syntax, causing errors.
  3. Final Answer:

    @Value("${server.port}") private int port; -> Option A
  4. Quick Check:

    Use quotes and ${} for property injection [OK]
Quick Trick: Always use @Value("${property}") syntax [OK]
Common Mistakes:
  • Omitting quotes around the property placeholder
  • Not using ${} for property reference
  • Using property name without quotes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes