Kayavak / The Backend / BE·46 Spring Core / Deeper
Deeper · The Backend
Spring Core
The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.
Why does this exist?
Enterprise Java around 2000 meant Enterprise JavaBeans: components that had to implement framework interfaces, needed an application server to run at all, and were configured with long XML descriptors. Simple business logic was hard to write and nearly impossible to unit-test. In 2002 Rod Johnson's book Expert One-on-One J2EE Design and Development argued that plain Java objects, wired together by a lightweight container, could do the same job; the code that came with it became the Spring Framework (1.0 in March 2004).
The central idea had several names. "Inversion of control" (the framework calls you, not the other way round) was already used in the 1990s; in January 2004 Martin Fowler named the specific pattern of handing objects their collaborators dependency injection. Spring's other pillar was proxies for cross-cutting concerns (transactions, security) applied without touching business code. Java EE eventually adopted the same ideas (CDI, EJB 3), and Spring Boot (ch47) made the container nearly invisible.
The mental model
Try it yourself first: write your own model in the Mental Model Explorer, then compare.
Spring is a factory for your objects. You declare what each object needs; the container builds the graph, wraps objects in proxies where you asked for extra behaviour, and hands out the wrapped versions.
| Question | Answer |
|---|---|
| What is it? | An inversion-of-control container plus libraries built on it |
| Problem it solves | Wiring an application's objects and adding cross-cutting behaviour without coupling code to the framework |
| Parts | Bean definitions, the ApplicationContext, dependency injection, post-processors, proxies and AOP, profiles, events |
| How it works | Read definitions, instantiate beans in dependency order, inject collaborators, let post-processors wrap them in proxies |
| Assumptions | Beans are called through the injected reference (the proxy) |
| When they change | A method calling another @Transactional method on this bypasses the proxy, so no transaction starts |
| Where it fails | Magic: behaviour from annotations and auto-configuration that's hard to trace; start-up time |
| Related ideas | Interfaces and composition (ch34); proxies as dynamic dispatch; testing with injected fakes (ch54) |
What if?
What if a @Transactional method is called from another method of the same class?
Reason it through, then open
The call goes straight to this, not through the proxy Spring wrapped around the bean, so the transaction advice never
runs: no transaction starts and nothing rolls back on error. It's the most common Spring surprise. Calling through
another bean, or moving the transactional method into its own service, routes the call through a proxy.
What if classes created their own dependencies with new?
Reason it through, then open
Each class would choose its collaborators' concrete types and configuration, so tests couldn't substitute fakes, and changing a database client would mean editing every class that creates one. Injection moves those decisions to one place (configuration), which is the whole point of the container.
Where the boundary is
| Aspect | Constructor injection | Field injection |
|---|---|---|
| Dependencies visible | In the constructor's signature | Hidden in private fields |
Can be final |
Yes | No |
| Testable without Spring | Yes: call the constructor | Needs reflection or the container |
| Recommended | Yes | No |
How sure are we?
| Claim | How it's known | Where from |
|---|---|---|
| Spring grew from Rod Johnson's 2002 book | Historical | The book and Spring's history |
| Fowler named "dependency injection" in January 2004 | Historical | Inversion of Control Containers and the Dependency Injection pattern |
| Self-invocation bypasses Spring's proxies | Convention | Spring's proxy-based AOP; AspectJ weaving behaves differently |
Still open
Compile-time dependency injection (Dagger, Micronaut) and Spring's ahead-of-time processing trade run-time flexibility for faster start-up; how much of Spring's reflection-based dynamism remains necessary is an active discussion.
What would change your mind? If an application stayed small and simple, manual wiring in a main method might be
clearer than any container. Containers pay off as the object graph grows.
Recall
Why doesn't a self-invoked `@Transactional` method start a transaction?
The call bypasses the proxy Spring wrapped around the bean.
What problem did Spring originally solve?
The complexity and untestability of EJB-era enterprise Java, using plain objects in a lightweight container.
Why prefer constructor injection?
Dependencies are explicit, can be final, and the class is testable without the container.
What does inversion of control mean?
The framework calls your code and supplies its collaborators, instead of your code creating them.
Reflect
Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.