Scrivio Interview library
DesignIntermediate6 min read · updated 12 November 2026

A colleague argues that Spring's IoC container adds hidden magic and that plain constructors with a hand-written main wiring class would be clearer for a small service. When do you agree?

I agree when the object graph is small and the service needs few integrations: a hand-written wiring class is explicit, starts in milliseconds and anyone can read it. Spring earns its place when we need what it integrates, such as transactions, security, configuration binding, Actuator health and metrics, and test slices. Because constructor injection keeps our classes free of Spring, we can start with manual wiring for a CLI tool or library and move to Spring later for a typical web service without rewriting the classes.

Why this question is asked

This is a real debate on many teams, and both sides are partly right. Interviewers ask it to see whether we can weigh a tool on its costs and benefits instead of defending it by habit. A good answer neither dismisses the colleague as old-fashioned nor agrees that Spring is just magic. In this answer, we will see what manual wiring looks like and what it does well, look at what Spring actually contributes beyond wiring, notice how constructor injection keeps the choice open, and then decide for a few concrete kinds of service.

1A Quick Recap: What Wiring Means

Wiring is the code that creates our objects and connects them: build the DataSource, pass it to the repository, pass the repository to the service, pass the service to the HTTP handler. Something must do it in every application. The question is whether we write it by hand in a main method, sometimes called the composition root, or let an IoC container such as Spring's ApplicationContext do it from annotations and configuration.

The colleague's word, hidden magic, refers to the fact that with Spring there is no single place where we can read new OrderService(repo). The container finds classes by scanning, decides which bean to inject by type, and wraps some beans in proxies. That is real indirection, and it does cost something to learn.

2What Manual Wiring Looks Like

Here is a small service wired by hand. Our classes use constructor injection and contain no framework annotations.

Java
public final class Main {
public static void main(String[] args) {
Config cfg = Config.fromEnv(); // reads DB_URL, PORT
DataSource ds = Pools.hikari(cfg.dbUrl(), 10); // one pool, 10 connections
OrderRepository repo = new JdbcOrderRepository(ds);
OrderService orders = new OrderService(repo, Clock.systemUTC());
HttpServer server = Http.server(cfg.port())
.route("POST /orders", new PlaceOrderHandler(orders));
Runtime.getRuntime().addShutdownHook(new Thread(() -> { server.stop(); Pools.close(ds); }));
server.start(); // listening after about 150 ms
}
}

Everything is visible in twenty lines. A new engineer can follow the graph with go-to-definition, the compiler checks every connection, and a missing dependency is a compile error rather than a startup exception. Startup is fast because nothing is scanned or reflected. For a graph of ten or twenty objects, this is honestly clearer than any container.

3What Spring Adds Beyond Wiring

The weakness of the comparison is that it treats Spring as only a wiring tool. Wiring is the easy part. What a typical web service needs is a long list of integrations, and each one we write by hand is code to maintain. Spring gives us declarative transactions with @Transactional, so commit and rollback are handled for every service method. Spring Security gives authentication, method security and protection against common attacks. Configuration binding with @ConfigurationProperties turns properties, environment variables and profiles into typed, validated objects. Actuator exposes health, readiness and metrics endpoints that our platform already expects. And the testing support, @SpringBootTest, @WebMvcTest, @DataJpaTest and Testcontainers integration, lets us test slices of the application with real wiring.

Each of those relies on the container owning object creation, because it works by wrapping beans in proxies or by reacting to beans being registered. With manual wiring we can still use libraries for each concern, but we wire, configure and test those integrations ourselves. So the real question is not magic versus clarity; it is how many of these integrations the service needs.

Cost and benefit by concern
ConcernManual wiringSpring Boot
Reading the object graphone main classspread across annotations
Startup timetens to hundreds of msone to several seconds, less with AOT
Transactionswrite try/commit/rollback@Transactional
Securitywire a library by handSpring Security filters and annotations
Config binding and profileshand-written parsing@ConfigurationProperties
Health and metricswrite endpointsActuator
Integration testsbuild our own fixturestest slices, Testcontainers support

4Constructor Injection Keeps the Choice Open

Notice that the classes in the manual example would work unchanged under Spring. OrderService takes its repository and a Clock in its constructor. Spring can call that same constructor; all it needs is to know the class is a bean, either through an @Bean method in a configuration class or a stereotype annotation. If we keep framework annotations out of domain classes and use constructor injection, the classes do not care who wires them.

This changes the debate in a useful way. We are not choosing an architecture for the life of the service. We are choosing who writes the wiring today. A tool can start with a Main class and move to Spring when it grows integrations, and the move only touches the wiring layer, not the business code.

Java
@Configuration(proxyBeanMethods = false)
class OrderConfig {
@Bean OrderService orderService(OrderRepository repo) {
return new OrderService(repo, Clock.systemUTC()); // the same constructor as in Main
}
}

5When I Agree, and When I Do Not

For a small command-line tool, a batch job with two dependencies, or a library that other teams embed, I agree with the colleague. A library in particular should not force a container on its users, and a CLI tool benefits from instant startup and a tiny dependency tree. Manual wiring is often the better choice there.

For a typical web service, with a database, transactions, security, several downstream clients, metrics and a platform that expects health endpoints, Spring pays off quickly. Hand-wiring all of that is not clearer; it just moves the complexity into code we own and must test. The hidden magic concern is better answered with team skills than by dropping the framework: knowing how to read the conditions report, how proxies work, and keeping configuration explicit with @Bean methods for infrastructure.

Step throughDeciding how to wire a service
  1. Is it a library others embed?
    YesManual wiring, no containerNoGo on
  2. Needs transactions, security or health endpoints?
    YesSpring BootNoGo on
  3. More than about 20 objects or several clients?
    YesSpring BootNoManual wiring in Main
  4. Either way: constructor injection, no framework types in domain codekeeps the switch cheap

4 steps in order. Play them, or step through them one at a time.

6Follow-up questions

Does Spring's startup cost matter for a web service?

Usually not for a long-running service that starts a few times a day; a few seconds is irrelevant against hours of uptime. It matters for serverless functions, short jobs and fast autoscaling. For those cases Spring AOT, class data sharing and native images reduce the cost a lot, and manual wiring or a build-time DI framework is also a reasonable choice.

How do we answer the hidden magic complaint without dropping Spring?

Make the magic visible. Run with --debug to see the conditions evaluation report, use the Actuator beans endpoint, prefer @Bean methods for infrastructure so it is reviewed like any other code, and keep component scanning limited to the application's own packages. Most of the surprise comes from not knowing where to look.

Is there a middle ground between the two?

Yes. Build-time DI tools such as Dagger generate the wiring code at compile time, so we get explicit, compiler-checked wiring without writing it by hand. Within Spring, using functional bean registration or explicit @Bean methods instead of scanning gives a similar explicitness.

7Conclusion

In this answer, we saw that manual wiring is explicit, starts fast and is easy to read when the object graph is small. Spring adds value through its integrations, transactions, security, configuration binding, Actuator and test support, which all depend on the container owning object creation. Constructor injection keeps classes free of Spring, so either approach can wire the same classes and the choice can change later. For a small CLI tool or a library, manual wiring is often better; for a typical web service with many integrations, Spring pays off.

What to say in the interviewAbout 90 seconds aloud

Open with

I agree with my colleague more often than people expect. For a small object graph, a hand-written main class that calls constructors is explicit, compiler-checked and starts in milliseconds, and anyone can follow it with go-to-definition.

Then, if they want more

For a command-line tool, a small batch job or a library other teams embed, I would actually choose that, and a library should never force a container on its users. Where I disagree is the idea that Spring is only wiring. Wiring is the easy part.

A typical web service needs declarative transactions, security, typed configuration from properties and environment variables, health and metrics endpoints our platform expects, and good integration test support. Spring gives all of that because the container owns object creation and can wrap beans in proxies. Writing those by hand does not remove the complexity, it moves it into code we own.

The useful point is that this is not a one-way decision. If our classes use constructor injection and keep framework annotations out of the domain, the same classes can be wired by a Main class today and by @Bean methods tomorrow.

So I would say: start simple when the service is small, switch when the integrations arrive, and answer the magic concern by teaching people to read the conditions report and keeping infrastructure in explicit @Bean methods.

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 documentation below. 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 "A colleague argues that Spring's IoC container adds hidden magic and that plain...": https://scrivio-interview-library.netlify.app/spring-boot/ioc-container/947d4c9d3c/