Bird
0
0

Which of the following is the correct syntax for field injection in a Spring Boot component?

easy📝 Syntax Q3 of 15
Spring Boot - Inversion of Control and Dependency Injection
Which of the following is the correct syntax for field injection in a Spring Boot component?
A@Autowired private MyService myService;
Bprivate MyService myService = new MyService();
C@Inject public MyService myService;
D@Service private MyService myService;
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct annotation for field injection

    Spring Boot uses @Autowired to inject dependencies on fields.
  2. Step 2: Check syntax correctness

    The field must be private and annotated with @Autowired for field injection.
  3. Final Answer:

    @Autowired private MyService myService; -> Option A
  4. Quick Check:

    Field injection syntax = A [OK]
Quick Trick: Use @Autowired on private fields for field injection [OK]
Common Mistakes:
  • Using new keyword instead of injection
  • Confusing @Inject with @Autowired in Spring Boot
  • Misusing @Service annotation on fields

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes