Bird
0
0

Which of the following is the correct way to use @Autowired for field injection in a Spring Boot class?

easy📝 Syntax Q12 of 15
Spring Boot - Inversion of Control and Dependency Injection
Which of the following is the correct way to use @Autowired for field injection in a Spring Boot class?
Aprivate Service service @Autowired;
B@Autowired private Service service;
CService @Autowired service;
Dprivate @Autowired Service service;
Step-by-Step Solution
Solution:
  1. Step 1: Recall field injection syntax and match correct syntax

    The @Autowired annotation is placed before the field declaration with its access modifier. @Autowired private Service service; correctly places @Autowired before the field type and name.
  2. Final Answer:

    @Autowired private Service service; -> Option B
  3. Quick Check:

    @Autowired before field = correct syntax [OK]
Quick Trick: Put @Autowired right before the field declaration [OK]
Common Mistakes:
  • Placing @Autowired after the field name
  • Putting @Autowired between type and variable name
  • Missing access modifier with @Autowired

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes