juniorSpring

What is Dependency Injection in Spring?

Updated May 17, 2026

Short answer

DI is a pattern where dependencies are provided to a class instead of created internally.

Deep explanation

Spring supports constructor, setter, and field injection. Constructor injection is preferred because it ensures immutability and easier testing.

Real-world example

A service receives repository objects instead of creating database connections itself.

Common mistakes

  • Using field injection everywhere which makes testing difficult.

Follow-up questions

  • Which injection type is recommended?
  • Why is DI important?

More Spring interview questions

View all →