Simple definition#
Dependency injection (DI) means an object receives the objects it works with — its dependencies — from outside, instead of creating or looking them up itself.
// Without DI: the service decides which notifier it uses.
private final Notifier notifier = new EmailNotifier();
// With DI: whoever creates the service decides.
public OrderService(Notifier notifier) { this.notifier = notifier; }
That is the entire idea. Frameworks such as Spring automate the wiring, but DI itself needs no framework.
The problem#
Here is an order service that creates its own collaborators:
package hub.di;
import java.time.Instant;
import java.util.UUID;
/**
* Before: the service creates its own collaborators. It works, but it cannot
* be tested without a real clock and real notifications, and every change of
* implementation means editing this class.
*/
public final class TightlyCoupledOrderService {
private final OrderRepository repository = new InMemoryOrderRepository();
private final Notifier notifier = new ConsoleNotifier();
public Order placeOrder(String customerEmail) {
Order order = new Order(UUID.randomUUID().toString(), customerEmail, Instant.now());
repository.save(order);
notifier.send(customerEmail, "Order " + order.id() + " received");
return order;
}
}It works, but:
- It cannot be tested in isolation. Every test sends a real notification and uses the real clock and random IDs, so assertions on time or IDs are impossible.
- Changing an implementation means editing this class. Moving from the in-memory repository to a database, or from console output to e-mail, requires modifying
TightlyCoupledOrderServiceitself. - Its dependencies are hidden. Nothing in its constructor tells you that it talks to a repository and sends notifications. You must read the implementation.
Why it matters#
DI is the practical mechanism behind the Dependency Inversion Principle. It makes Strategy-style variation configurable (Strategy pattern), it is the foundation of the Spring Framework, and it is what lets Hexagonal Architecture test business logic without a database.
Core concept#
Inversion of Control#
DI is one form of Inversion of Control (IoC): control over which object is used moves from the class that uses it to the code that assembles the application. Other forms of IoC include template methods, callbacks and event listeners — “don’t call us, we’ll call you”.
flowchart LR
subgraph Before["Without DI"]
direction TB
S1["TightlyCoupledOrderService"] -- "new" --> R1["InMemoryOrderRepository"]
S1 -- "new" --> N1["ConsoleNotifier"]
end
subgraph After["With DI"]
direction TB
Root["Composition root<br/>(main method or DI container)"] -- "new" --> R2["InMemoryOrderRepository"]
Root -- "new" --> N2["ConsoleNotifier"]
Root -- "new, passing both" --> S2["OrderService"]
S2 -. "uses via interface" .-> R2
S2 -. "uses via interface" .-> N2
endThree ways to inject#
| Style | How | Verdict |
|---|---|---|
| Constructor injection | Dependencies are constructor parameters | Default choice. Dependencies are explicit and can be final; the object is complete once constructed. |
| Setter injection | A setter is called after construction | For genuinely optional dependencies that have a sensible default. |
| Field injection | A framework writes directly into a private field (for example, @Autowired on a field) | Avoid in production code. Dependencies are hidden, fields cannot be final, and the class cannot be created without the framework. |
The composition root#
The composition root is the one place where the application’s object graph is assembled — usually close to main. Everything else receives its dependencies and never creates them. In a Spring application, the container plus its @Configuration classes form the composition root.
Practical example#
The refactored service receives four dependencies: a repository, a notifier, a java.time.Clock and an ID generator.
package hub.di;
import java.time.Clock;
import java.time.Instant;
import java.util.Objects;
import java.util.function.Supplier;
/**
* After: every collaborator arrives through the constructor. The class states
* its dependencies honestly, they can be final, and it is trivial to test.
*/
public final class OrderService {
private final OrderRepository repository;
private final Notifier notifier;
private final Clock clock;
private final Supplier<String> idGenerator;
public OrderService(OrderRepository repository, Notifier notifier, Clock clock, Supplier<String> idGenerator) {
this.repository = Objects.requireNonNull(repository);
this.notifier = Objects.requireNonNull(notifier);
this.clock = Objects.requireNonNull(clock);
this.idGenerator = Objects.requireNonNull(idGenerator);
}
public Order placeOrder(String customerEmail) {
Order order = new Order(idGenerator.get(), customerEmail, Instant.now(clock));
repository.save(order);
notifier.send(customerEmail, "Order " + order.id() + " received");
return order;
}
}The interfaces it depends on:
package hub.di;
import java.util.Optional;
public interface OrderRepository {
void save(Order order);
Optional<Order> findById(String id);
}package hub.di;
public interface Notifier {
void send(String recipient, String message);
}The composition root wires the service twice: once as production would, and once as a test would.
package hub.di;
import java.time.Clock;
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
/** The composition root: the one place that knows which implementations are used. */
public final class DiDemo {
public static void main(String[] args) {
// Production-style wiring (what a DI container automates).
OrderService production = new OrderService(
new InMemoryOrderRepository(),
new ConsoleNotifier(),
Clock.systemUTC(),
() -> UUID.randomUUID().toString());
Order real = production.placeOrder("ana@example.com");
System.out.println("Production wiring placed an order with a random id: " + (real.id().length() == 36));
// Test-style wiring: deterministic clock, predictable ids, recording notifier.
Clock fixed = Clock.fixed(Instant.parse("2026-01-15T10:00:00Z"), ZoneOffset.UTC);
AtomicInteger seq = new AtomicInteger();
List<String> sent = new ArrayList<>();
InMemoryOrderRepository repository = new InMemoryOrderRepository();
OrderService underTest = new OrderService(
repository,
(to, msg) -> sent.add(to + ": " + msg),
fixed,
() -> "ORD-" + seq.incrementAndGet());
Order order = underTest.placeOrder("ben@example.com");
System.out.println("Placed " + order.id() + " at " + order.placedAt());
System.out.println("Stored: " + repository.findById("ORD-1").isPresent());
System.out.println("Notifications: " + sent);
}
}Expected output ({{any}} stands for the random UUID, which differs on every run):
[notify ana@example.com] Order {{any}} received
Production wiring placed an order with a random id: true
Placed ORD-1 at 2026-01-15T10:00:00Z
Stored: true
Notifications: [ben@example.com: Order ORD-1 received]Implementation walkthrough#
- Turn hidden dependencies into parameters. Anything the class creates with
new, looks up statically or reads from the environment — including the current time and random IDs — is a dependency. - Depend on abstractions where variation is real.
OrderRepositoryandNotifierhave several realistic implementations. Time and ID generation use the JDK’s own abstractions,ClockandSupplier<String>, so no new interfaces are needed. - Validate once, in the constructor.
Objects.requireNonNullmakes a wiring mistake fail at startup, not on the first order. - Make dependencies
final. The service is fully initialised after construction and safe to share between threads, provided its dependencies are thread-safe. - Wire in one place.
DiDemo.mainis the composition root. It is the only code that names concrete classes. - Substitute freely in tests. The test wiring uses a fixed clock, a sequential ID generator and a lambda that records notifications. No mocking library is needed, and the assertions are deterministic.
The same design with Spring#
A DI container automates the composition root. With Spring Framework 7 (Spring Boot 4), the service class needs no Spring annotations at all if you register it in a configuration class:
@Configuration(proxyBeanMethods = false)
class OrderConfiguration {
@Bean
Clock clock() {
return Clock.systemUTC();
}
@Bean
OrderService orderService(OrderRepository repository, Notifier notifier, Clock clock) {
return new OrderService(repository, notifier, clock, () -> UUID.randomUUID().toString());
}
}
Alternatively, you can annotate the class itself with @Service. If a class has a single constructor, Spring uses it for injection automatically; @Autowired is unnecessary (this has been true since Spring Framework 4.3).
@Service
public class OrderService {
private final OrderRepository repository;
// ...
public OrderService(OrderRepository repository, Notifier notifier, Clock clock, Supplier<String> ids) { ... }
}
The first style keeps framework annotations out of business classes entirely. The Hexagonal Architecture example uses it for its application core, in UseCaseConfiguration:
package com.example.orders.config;
import com.example.orders.application.port.out.OrderRepository;
import com.example.orders.application.port.out.PaymentPort;
import com.example.orders.application.service.OrderApplicationService;
import com.example.orders.domain.OrderId;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.time.Clock;
/**
* Composition root for the application core. The core has no Spring
* annotations; this class is the only place that turns it into beans.
*/
@Configuration(proxyBeanMethods = false)
class UseCaseConfiguration {
@Bean
Clock clock() {
return Clock.systemUTC();
}
@Bean
OrderApplicationService orderApplicationService(OrderRepository orders, PaymentPort payments, Clock clock) {
return new OrderApplicationService(orders, payments, clock, OrderId::newId);
}
}What the container adds beyond plain DI:
- Automatic wiring by type, with qualifiers when several candidates exist.
- Lifecycle management: singleton and other scopes, initialisation and destruction callbacks.
- Proxies that add transactions, security and caching around beans — see Patterns in Spring.
- Configuration: profiles and externalised properties to choose implementations per environment.
Real-world applications#
- Swapping an in-memory repository for a JPA repository between tests and production.
- Choosing a payment provider adapter per environment or per tenant.
- Injecting
Clockto test expiry, scheduling and “orders older than 30 days” logic. - Injecting HTTP clients configured with timeouts and retries, so business code does not build them.
Advantages and disadvantages#
| Advantages | Disadvantages |
|---|---|
| Classes are testable in isolation with simple fakes | Wiring moves elsewhere; you must look in the composition root or container to see the full graph |
| Dependencies are explicit in constructors | A container adds startup cost, reflection or code generation, and “magic” that must be learned |
| Implementations can change without editing clients | Overuse creates interfaces with one implementation and deep object graphs |
| Supports SOLID’s DIP and OCP | Misconfiguration surfaces at startup, or at runtime with lazy beans, rather than at compile time |
Alternatives and comparisons#
| Approach | How a class obtains a dependency | Main drawback |
|---|---|---|
Direct instantiation (new) | Creates it | Tight coupling, untestable |
| Singleton / static access | Calls Foo.getInstance() | Hidden global state shared between tests |
| Service Locator | Asks a registry: locator.get(Foo.class) | Dependencies are hidden inside method bodies; the class depends on the locator |
| Dependency injection | Receives it | Wiring lives elsewhere |
DI versus DIP. The Dependency Inversion Principle is a design rule: high-level policy should depend on abstractions. Dependency injection is a technique for supplying those abstractions. You can inject concrete classes (DI without DIP), and you could satisfy DIP using a service locator (DIP without DI). They work best together.
Common mistakes#
- Too many constructor parameters. Seven dependencies is a design smell, not a DI problem. The class probably has more than one responsibility; split it.
- An interface for every class. Introduce an interface where there is real variation or a boundary (a port), not by reflex.
- Injecting the container. Passing
ApplicationContextinto a class and callinggetBeanturns DI back into a service locator. - Circular dependencies. A needs B and B needs A. Spring Boot prohibits circular references by default since version 2.6. Fix the design — usually by extracting a third class or using an event.
- Hidden statics.
LocalDate.now()deep inside a method is still a dependency. Inject aClock.
Interview questions and answers#
What is dependency injection? A technique in which an object receives its dependencies from outside, through its constructor, a setter or a field, instead of creating them. It decouples a class from the concrete implementations it uses.
Why is constructor injection preferred?
Dependencies are explicit and can be final. The object is complete and valid after construction, and it can be created in a plain unit test without a framework. Missing dependencies fail fast at startup.
What is the difference between IoC and DI? Inversion of Control is the general principle that a framework or caller controls the flow or the choice of collaborators. DI is one specific form of IoC that concerns how dependencies are supplied.
What is the difference between DI and the Dependency Inversion Principle? DIP is a design principle about the direction of source-code dependencies. DI is a technique for providing dependencies. DI is commonly used to wire a design that follows DIP.
How do you handle two beans of the same type in Spring?
Use @Qualifier at the injection point, mark one implementation @Primary, inject a List or Map of all implementations, or define the beans explicitly in a configuration class.
Do you need Spring to do dependency injection?
No. Constructors plus one composition root — usually main — are plain DI. A container automates wiring and adds lifecycle management, scopes and proxies.
Summary#
- DI means receiving dependencies instead of creating them.
- Constructor injection is the default: dependencies are explicit,
finaland validated once. - One composition root assembles the object graph; in Spring, that is the container and its configuration.
- Treat time, randomness and environment access as dependencies too.
- DI is a technique; DIP is the principle it usually serves.
Related topics#
- SOLID Principles: Dependency Inversion
- Strategy Pattern
- Hexagonal Architecture with Spring Boot
- Patterns in Spring
References#
- Martin Fowler. “Inversion of Control Containers and the Dependency Injection pattern”, 2004.
- Mark Seemann and Steven van Deursen. Dependency Injection: Principles, Practices, and Patterns. Manning, 2019.
- Spring Framework reference: Dependency Injection.
- Spring Boot reference: Spring Beans and Dependency Injection.