Scrivio Interview library
DesignIntermediate6 min read · updated 12 November 2026

Without any DI framework, show how you would wire a small Java application of a controller, a service, a repository and a clock using dependency injection. What is a composition root?

Without a framework, each class receives its dependencies as constructor parameters and never creates them itself: the controller gets the service, the service gets the repository and a java.time.Clock. A composition root is the one place, usually main(), where the application creates all these objects and connects them. Tests skip the composition root and build objects directly with fakes such as Clock.fixed(...), which shows that dependency injection is a pattern and a container is only one way to automate the wiring.

Why this question is asked

Many developers learn dependency injection through Spring and come to think the two are the same thing. Interviewers ask this question to see whether we understand the pattern underneath, because that understanding is what lets us write testable libraries, debug wiring problems and judge when a container is worth it. In this answer, we will recap what dependency injection means, write a small application with a controller, a service, a repository and a clock, build a composition root in main(), handle values that are only known at run time, write a test with a fixed clock, and finish with what this means for libraries.

1A Quick Recap of the Pattern

A dependency is an object another object needs to do its work. Dependency injection means the object does not create or look up its dependencies; it receives them, most often through its constructor. The class says what it needs and someone else decides which implementation it gets.

The benefit is that the decision moves to one place. The service does not know whether its repository talks to PostgreSQL or to an in-memory map, and it does not know whether its clock is the real system clock or a frozen one in a test.

2Writing the Classes

Each class declares its dependencies as constructor parameters and stores them in final fields. Notice that none of them calls new on a collaborator.

Java
interface OrderRepository {
void save(Order order);
Optional<Order> find(String id);
}
final class JdbcOrderRepository implements OrderRepository {
private final DataSource dataSource;
JdbcOrderRepository(DataSource dataSource) { this.dataSource = dataSource; }
public void save(Order order) { /* insert with JDBC */ }
public Optional<Order> find(String id) { /* select with JDBC */ return Optional.empty(); }
}
final class OrderService {
private final OrderRepository repository;
private final Clock clock;
OrderService(OrderRepository repository, Clock clock) { this.repository = repository; this.clock = clock; }
Order place(String customer, BigDecimal total) {
Order o = new Order(UUID.randomUUID().toString(), customer, total, Instant.now(clock));
repository.save(o);
return o;
}
}
final class OrderController {
private final OrderService service;
OrderController(OrderService service) { this.service = service; }
String post(String customer, String total) { return service.place(customer, new BigDecimal(total)).id(); }
}

The service takes a Clock rather than calling Instant.now() directly. The reason is that time is a hidden dependency: code that reads the system clock cannot be tested for a specific date without waiting for that date. Passing a Clock makes time an ordinary, replaceable dependency. Creating the Order with new is fine, because Order is a value the service produces, not a collaborator it depends on.

3The Composition Root in main()

Someone still has to create the real objects. The composition root is the single place where that happens, as close to the entry point of the application as possible. It is the only code that knows about concrete classes such as JdbcOrderRepository.

Java
public final class Main {
public static void main(String[] args) {
DataSource ds = dataSource(System.getenv("DB_URL"));
Clock clock = Clock.systemUTC();
OrderRepository repository = new JdbcOrderRepository(ds);
OrderService service = new OrderService(repository, clock);
OrderController controller = new OrderController(service);
HttpServerStarter.start(8080, controller); // the application is now wired and running
}
}
Step throughWho creates what
  1. Main (composition root)creates every object and connects them, knows the concrete classes
  2. OrderControllerreceives OrderService
  3. OrderServicereceives OrderRepository and Clock
  4. JdbcOrderRepository and Clockreceive DataSource; the clock is Clock.systemUTC()

4 layers, from the top down. Step through them one at a time.

Only the top layer uses new on collaborators; every other class receives what it needs.

Notice the order: dependencies are created before the objects that need them, so we build from the bottom up. If we forget one, the code does not compile, which is a nice property compared with a container that reports a missing bean at startup. With four classes this is a dozen lines. With 200 classes, writing it by hand becomes tedious, and that is exactly the work a container such as Spring, Guice or Dagger automates. It reads the same information, the constructor parameters, and does the wiring for us.

4Values Known Only at Run Time

Some values do not exist when the application starts: the id of the current user, the file a request uploaded, the tenant of this call. These must not be constructor parameters of long-lived objects. We pass them as method parameters, as in service.place(customer, total), or, when we need a new object per request, we inject a factory that creates it.

A factory here is just a small object or a function that the composition root wires once and that creates request-scoped objects later. For example, the composition root can inject a Function<String, ReportWriter> that creates a writer for a given file name, so the service asks for a writer when it has the name, and still never calls new ReportWriter itself.

5Testing With Fakes and a Fixed Clock

A test is a second, tiny composition root. It wires the same classes with fakes instead of real infrastructure.

Java
@Test
void stampsOrderWithCurrentTime() {
Clock fixed = Clock.fixed(Instant.parse("2026-01-15T10:00:00Z"), ZoneOffset.UTC);
var repo = new InMemoryOrderRepository();
var service = new OrderService(repo, fixed);
Order o = service.place("acme", new BigDecimal("42.00"));
assertEquals(Instant.parse("2026-01-15T10:00:00Z"), o.createdAt()); // passes every time
assertEquals(1, repo.count()); // 1
}

No database, no container, no reflection and no waiting. The test runs in milliseconds and is deterministic, because time is whatever we decide. This is the strongest argument for the pattern: when every class receives its dependencies, every class can be tested on its own.

6What This Means for Libraries

A library that accepts its dependencies through constructors works in any application: one wired by hand, one using Spring, one using Dagger or Quarkus. A library that requires a specific container, for example by injecting its own fields with @Autowired or looking beans up from an ApplicationContext, forces that container on every user. So libraries should expose plain constructors or builders and, if convenient, offer optional framework integration in a separate module, such as a Spring Boot auto-configuration that simply calls those constructors.

7Follow-up questions

When is hand wiring better than a container?

For small applications, command line tools, libraries and services that need very fast startup, hand wiring is simple and transparent, and errors show at compile time. Once there are many classes, configuration profiles and cross-cutting features such as transactions, a container saves a lot of code. The classes look the same either way, so we can switch later.

Is the composition root the same as a service locator?

No. A service locator is an object classes call to fetch their dependencies, which hides those dependencies inside method bodies. A composition root pushes dependencies in from the outside, and no other class ever talks to it.

Where does configuration such as the database URL go?

It is read in the composition root, from environment variables or a file, and turned into objects such as the DataSource before anything else is created. The rest of the code receives ready objects and never reads configuration directly.

8Conclusion

In this answer, we wired a controller, a service, a repository and a clock without any framework. Each class receives its dependencies through constructor parameters and never creates them, and a composition root in main() creates all objects and connects them in one place. Values known only at run time travel through method parameters or injected factories, and tests build objects directly with fakes and a fixed Clock. Dependency injection is the pattern; a container only automates the wiring, and libraries should accept dependencies this way so they work with any container.

What to say in the interviewAbout 80 seconds aloud

Open with

I would write every class to receive its dependencies through its constructor and never create its collaborators. So the repository takes a DataSource, the service takes the repository interface and a java.time.Clock, and the controller takes the service.

Then, if they want more

The Clock is important, because calling Instant.now directly is a hidden dependency on the system time that makes tests non-deterministic. Then the composition root, normally main, is the one place that creates all of these objects, bottom up: the DataSource and Clock.systemUTC first, then the repository, the service and the controller, and finally it starts the server.

It is the only code that knows the concrete classes. Values that only exist at request time, like the user or an uploaded file, go through method parameters or through a factory the root wires once.

A test is just a small composition root of its own: I build the service with an in-memory repository and Clock.fixed, and assert exact timestamps without Spring or reflection. That shows that dependency injection is the pattern and a container like Spring only automates this wiring.

It's also why libraries should take dependencies through constructors rather than depend on a particular container.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Without any DI framework, show how you would wire a small Java application of a...": https://scrivio-interview-library.netlify.app/java/creational-patterns/9e77f92d9b/