Bird
0
0

What issue can occur if a Spring Boot service annotated with @Service is instantiated manually using 'new' instead of relying on Spring's dependency injection?

medium📝 Debug Q7 of 15
Spring Boot - Advanced Patterns
What issue can occur if a Spring Boot service annotated with @Service is instantiated manually using 'new' instead of relying on Spring's dependency injection?
AThe service will be registered twice in the application context
BSpring will not manage the manually created instance, causing missing dependencies and lifecycle management
CThe application will throw a compilation error
DThe service will automatically become a prototype scoped bean
Step-by-Step Solution
Solution:
  1. Step 1: Role of @Service annotation

    @Service marks a class for Spring to manage as a singleton bean by default.
  2. Step 2: Effect of manual instantiation

    Using 'new' bypasses Spring's container, so dependencies and lifecycle callbacks are not applied.
  3. Final Answer:

    Spring will not manage the manually created instance, causing missing dependencies and lifecycle management -> Option B
  4. Quick Check:

    Manual instantiation bypasses Spring's DI container [OK]
Quick Trick: Always let Spring instantiate beans to enable DI [OK]
Common Mistakes:
  • Thinking manual instantiation registers bean twice
  • Expecting compilation errors from manual instantiation
  • Assuming manual instantiation changes bean scope

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes