Bird
0
0

In a Spring Boot application, you have a class annotated with @Component that needs to be injected into a @Service class. What must you ensure for this injection to work correctly?

hard📝 Application Q9 of 15
Spring Boot - Spring Annotations
In a Spring Boot application, you have a class annotated with @Component that needs to be injected into a @Service class. What must you ensure for this injection to work correctly?
AThe @Service class must be annotated with @Controller
BThe @Component class must be annotated with @Repository instead
CBoth classes must be in packages scanned by Spring's component scan
DNo additional configuration is needed if classes are public
Step-by-Step Solution
Solution:
  1. Step 1: Understand component scanning

    Spring only detects beans in packages included in component scanning.
  2. Step 2: Ensure both classes are scanned

    Both the @Component and @Service classes must be in scanned packages for injection to work.
  3. Final Answer:

    Both classes must be in packages scanned by Spring's component scan -> Option C
  4. Quick Check:

    Component scan includes classes = injection works [OK]
Quick Trick: Keep all beans in scanned packages for Spring to detect them [OK]
Common Mistakes:
  • Changing annotations unnecessarily
  • Assuming public class is enough
  • Mixing @Service and @Controller

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes