Quick revision
The short answer
I would agree a short written standard, constructor injection everywhere, no static ApplicationContext access and no new mutable static singletons, and then make it enforceable without a big-bang rewrite. Architecture tests such as ArchUnit fail the build on new violations while a frozen list exempts existing ones, teams fix old code when they touch it and first where it causes test or incident pain, examples and automated refactoring make the fix cheap, and a per-team violation count shows progress. Shared libraries are moved off the Spring container so they stay usable outside it.
What to remember
In this answer, we standardised dependency handling across 20 teams without stopping feature work. A short written standard sets constructor injection, no static context access and no new mutable static singletons. Frozen ArchUnit rules fail the build on new violations while existing ones are exempted, and old code is fixed when touched and first where it causes test or incident pain, helped by examples and automated refactoring. A per-team violation count shows progress, and shared libraries are kept free of the Spring container so they work anywhere.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I would agree a short written standard, constructor injection everywhere, no static ApplicationContext access and no new mutable static singletons, and then make it enforceable without a big-bang rewrite. Architecture tests such as ArchUnit fail the build on new violations while a frozen list exempts existing ones, teams fix old code when they touch it and first where it causes test or incident pain, examples and automated refactoring make the fix cheap, and a per-team violation count shows progress. Shared libraries are moved off the Spring container so they stay usable outside it.
Why this question is asked
In a large code base, three ways of getting dependencies usually means three generations of developers, and each style has its own failure mode: static context lookups that break in tests, singletons that leak state between tests, and hidden dependencies nobody can mock. Nobody will approve stopping feature work for a quarter to fix it. Interviewers ask this to see how a senior engineer changes a shared code base gradually and measurably. In this answer, we will recap why the mix hurts, write the standard, see how architecture tests stop new violations, plan the gradual refactoring, and decide how to track it and what to do with shared libraries.
1A Quick Recap: Why the Mix Hurts
Constructor injection is the style where Spring passes dependencies into the constructor; the class's needs are visible and it can be tested with new. A static ApplicationContext lookup is a call such as SpringContext.getBean(PaymentClient.class) through a static holder; the dependency is hidden inside a method body, and the code only works when a Spring context has been started and stored in a static field. A hand-written singleton is a class with a static INSTANCE field that creates itself; if it holds mutable state, that state is shared by every test in the same JVM.
The concrete pain shows up as tests that pass alone and fail together, because a singleton kept state from the previous test, and as unit tests that must start a whole Spring context just to satisfy one static lookup, adding minutes to every build.
2Writing a Standard People Can Follow
The standard should fit on one page. New and changed code uses constructor injection. No code calls a static holder of the ApplicationContext, and no new static holders are written. No new static singletons with mutable state; a stateless utility class with static methods is fine. The page shows a before and after example of each case and the most common fix, which is to turn the static lookup into a constructor parameter.
We agree it with the teams instead of announcing it, ideally through the architecture group where each team has a voice. A standard that teams helped write is followed; one that arrives by email is ignored.
3Stopping New Violations With Architecture Tests
An architecture test is a unit test that checks the structure of the code rather than its behaviour. ArchUnit, a Java library for this, can express our rules in a few lines. The key to adopting it in a large existing code base is to freeze the current violations: the existing ones are recorded in a file and tolerated, and only new ones fail the build.
Notice what freezing gives us: the count can only go down. Nobody is forced to fix old code today, but nobody can add new problems either. The exact ArchUnit API for method-call conditions differs a little between versions, so the snippet is a sketch to check against the version in use; the idea of a frozen rule is the important part.
4Refactoring While Touching Code, Pain First
With new violations stopped, the old ones are fixed in two ways. The first is the boy scout rule: when a team changes a class for a feature, it also converts that class to constructor injection, which usually takes minutes and is reviewed with the feature. The second is targeted work on the classes that cost the most, found from data rather than opinion: the singletons behind flaky tests, the static lookups that force slow Spring context tests, and the classes involved in recent incidents.
Automated refactoring makes the common fixes cheap. OpenRewrite, a tool that applies refactoring recipes across a code base, has Spring-related recipes; whether one fits field-to-constructor injection for our code is worth checking for the version in use. IDE refactorings and Lombok's @RequiredArgsConstructor also make the conversion quick in services that already use Lombok.
- Architecture test finds a violation
- Is it new in this pull request?compared with the frozen storeYesBuild fails, author fixes it nowNoTolerated in the frozen store
- Is the class being changed anyway?boy scout ruleYesConvert it with the featureNoLeave it for now
- Does it cause test or incident pain?flaky tests, slow context tests, incidentsYesSchedule it as targeted workNoWait for the next touch
4 steps in order. Play them, or step through them one at a time.
5Tracking Progress and Celebrating It
A number makes the work visible. Each build publishes the count of frozen violations per team, and a simple dashboard shows the trend, for example 412 at the start, 260 after one quarter. Teams that drop the most get mentioned in the engineering update. This sounds small, but recognition is often what keeps a background effort going when feature pressure is high. We would not turn it into a punishing target; the aim is a falling trend, not zero by a date.
6Keeping Shared Libraries Free of the Container
Shared libraries deserve special attention, because their style spreads to every consumer. A library that does a static getBean() lookup only works inside a Spring application that has set up that holder. Libraries should take their dependencies through constructors or builders, with no static context access, and offer an optional Spring Boot auto-configuration module that wires them for Spring users. Then the same library works in a batch job, a plain Java tool or a test without starting a container, and the library team can be the first to show the standard in practice.
7Follow-up questions
What if a static lookup is needed in a class Spring does not create, such as a JPA entity listener?
First check whether the framework can inject it: Hibernate and Spring can inject Spring beans into entity listeners and validators in current versions, check your version. If not, keep the lookup in one small, documented adapter class and list it as a permanent exemption, so the rest of the code stays clean.
How do you convince teams under deadline pressure?
By making the cost near zero for them. New violations are blocked automatically, old ones are fixed only when touched or when they are causing pain, and recipes do most of the editing. Showing that a frozen singleton was behind a specific flaky test or incident persuades more than any principle.
Are all singletons banned?
No. Spring beans are singletons by default, and that is fine. The problem is hand-written static singletons with mutable state, which cannot be replaced in tests and share state across them. Stateless static utilities are acceptable.
8Conclusion
In this answer, we standardised dependency handling across 20 teams without stopping feature work. A short written standard sets constructor injection, no static context access and no new mutable static singletons. Frozen ArchUnit rules fail the build on new violations while existing ones are exempted, and old code is fixed when touched and first where it causes test or incident pain, helped by examples and automated refactoring. A per-team violation count shows progress, and shared libraries are kept free of the Spring container so they work anywhere.
Open with
I would start with a one-page standard agreed with the teams: constructor injection for all new and changed code, no static ApplicationContext lookups, and no new static singletons holding mutable state, with before-and-after examples.
Then, if they want more
Then I'd make it enforceable without blocking anyone. ArchUnit lets me write those rules as tests, and FreezingArchRule records the existing violations, a few hundred probably, so only new ones fail the build and the count can only go down.
Old code gets fixed two ways: teams convert a class whenever they touch it for a feature, which is usually a few minutes, and we target the classes that actually hurt, like singletons behind flaky tests or static lookups that force slow full-context tests. Automated refactoring, OpenRewrite recipes or IDE refactorings, keeps the cost low.
I'd publish the violation count per team from the build and call out teams that bring it down, because visible progress keeps background work alive. Finally, shared libraries get priority: they should take dependencies through constructors and ship an optional Spring Boot auto-configuration, so they work outside the container too.
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 "A large Spring codebase owned by 20 teams mixes constructor injection, static...": https://scrivio-interview-library.netlify.app/java/creational-patterns/a50173d10e/