Dependency injection

Dependency injection means providing an object with its collaborators from the outside, typically through the constructor, instead of having it create them itself with new. The code then depends on interfaces and can receive a fake in tests. A container like Spring can assemble the objects.

Example

class Service {
    private final Repo repo;
    Service(Repo repo) { this.repo = repo; }
}

Related concepts

The whole glossary