← Back to Java

Dependency Injection in Java

JavaMidjava

The Question

What is dependency injection in Java?

What a Strong Answer Covers

  • IoC inversion of control
  • objects provided externally not created internally
  • constructor injection
  • setter injection
  • loose coupling
  • easier unit testing
  • Spring @Autowired

Senior-Level Answer

Dependency injection (DI) is a design pattern implementing the Inversion of Control (IoC) principle. Instead of a class creating its own dependencies with new, those dependencies are provided (injected) from outside — by a framework, factory, or test harness. The class declares what it needs; something else decides what to provide.

There are three injection styles. Constructor injection passes dependencies through the constructor: the object is fully initialized on creation, all dependencies are visible, and the class can be declared immutable. This is the preferred style. Setter injection provides dependencies through setter methods after construction, useful for optional dependencies. Field injection injects directly into annotated fields — convenient but problematic for testing because the field is not accessible without a DI framework or reflection.

Spring Framework is the dominant DI container in the Java ecosystem. It manages bean lifecycle and wiring. With Spring, you annotate a class with @Component (or @Service, @Repository) and declare dependencies with @Autowired on constructors, setters, or fields. The Spring IoC container creates and wires beans at application startup. Java EE (now Jakarta EE) provides the CDI specification with @Inject for the same purpose.

The core benefit is testability. A class that creates its own dependencies is hard to unit test because you cannot substitute a mock. A class that accepts its dependencies via constructor can be instantiated in a test with mock objects, isolating behavior entirely. The second benefit is decoupling: your class depends on an interface, not a concrete implementation. The injected concrete type can be swapped without changing the class.

DI also centralizes configuration: which implementations to use, lifecycle scope (singleton, prototype, request-scoped), and initialization order are managed in one place rather than scattered across the codebase.

What Separates a 2/3 from a 3/3

2/3 — Passing but Incomplete

Explains that dependencies are provided externally rather than created internally, names the three injection styles, and gives testability as the key benefit.

3/3 — Strong Answer

Adds the IoC principle relationship, explains why constructor injection is preferred (immutability, visibility), names Spring/@Autowired or Jakarta CDI/@Inject, and explains the interface-based decoupling benefit.

Common Mistakes

  • Conflating Dependency Injection with the Dependency Inversion Principle — DI is one way to achieve DIP but they are not the same thing.
  • Recommending field injection as the default — it hides dependencies and requires a DI container or reflection to test.
  • Not explaining why DI improves testability — just saying 'it's better' without demonstrating the mock substitution scenario.

Follow-Up Questions

  • Why is constructor injection preferred over field injection? — Constructor injection makes dependencies explicit, allows immutable fields, enables testing without a container, and fails fast if dependencies are missing.
  • What is the difference between IoC and DI? — IoC is the broad principle that a framework controls flow and object creation. DI is one specific pattern implementing IoC — objects receive dependencies rather than fetching them.
  • How would you implement a simple DI container without a framework? — A factory or registry that maps interface types to concrete implementations, instantiates them (once for singletons), and passes them to constructors of dependent classes.

Related Questions

  • JVM vs JRE vs JDK
  • Java Platform Independence
  • How JVM Works
  • Main Features of Java
  • public static void main

Can You Explain This Cold?

Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.

Get Graded — Free Assessment
GrindQuestionsAITechnical interview assessment
TermsPrivacyAbout