Scrivio Interview library
DebuggingFoundations6 min read · updated 12 November 2026

A singleton service injects a prototype-scoped ReportBuilder with @Autowired, expecting a new builder for each call, but data from one report appears in the next. Why, and how do you fix it?

Dependency injection happens only once, when the singleton service is created, so the service receives one ReportBuilder and keeps it for its whole life, and every report is built on that same instance with the previous report's data still inside. The fix is to ask for a new builder at the moment we need one: inject ObjectProvider<ReportBuilder> and call getObject() for each report, or use @Lookup method injection or jakarta.inject.Provider. A prototype scoped proxy is usually not the answer, because it creates a new instance on every method call.

Why this question is asked

This is one of the most common scope bugs in Spring applications, and it is easy to miss in tests that build only one report. The bean is correctly declared as a prototype, the code looks right, and yet the second report contains rows from the first. Interviewers ask it to check whether we understand when injection happens and what scope really controls. In this answer, we will reproduce the bug, explain why the prototype is created only once, go through the fixes with code, and see why a prototype scoped proxy is a trap here.

1Reproducing the Bug

A ReportBuilder collects rows and builds a report. Because it holds state, it is declared as a prototype. A singleton ReportService injects it with @Autowired.

Java
@Component
@Scope("prototype")
class ReportBuilder {
private final List<String> rows = new ArrayList<>();
ReportBuilder add(String row) { rows.add(row); return this; }
Report build() { return new Report(List.copyOf(rows)); }
}
@Service
class ReportService {
@Autowired private ReportBuilder builder; // looks like "a new builder"
Report daily(List<String> rows) {
rows.forEach(builder::add);
return builder.build();
}
}
service.daily(List.of("a", "b")); // Report[rows=[a, b]]
service.daily(List.of("c")); // Report[rows=[a, b, c]] <- data from the first report

Notice that the second report contains a and b. The builder is the same object both times, and its list was never cleared. In a web application this is even worse, because concurrent requests share the builder and corrupt each other's reports.

2Why the Prototype Is Created Only Once

Prototype scope means the container creates a new instance every time someone asks it for the bean. The question is when ReportService asks. It asks exactly once: when the container creates the ReportService singleton at startup, it resolves the @Autowired field, requests a ReportBuilder, receives a fresh one, and stores it in the field. After that, the container is never asked again. Calling builder.add() is an ordinary Java call on the object in the field.

So the scope did its job: it gave a new instance on the one request it received. What we really wanted was a new instance per report, and that needs a request to the container per report. The fix is to replace "inject a builder" with "inject something that can give me a new builder whenever I ask".

Step throughInjection happens once at startup
ContainerReportService (singleton)ReportBuilder #1
create ReportBuilder #1 (prototype request)
create ReportService, inject #1
daily(a, b): add, add, build
daily(c): add, build -> still has a, b
never asks the container again

5 messages in order. Play them, or step through them one at a time.

The prototype scope only acts when the container is asked.

3Fix 1: ObjectProvider

ObjectProvider<T> is a Spring interface for lazy, on-demand access to a bean. Injecting it is cheap, and each call to getObject() asks the container for the bean, which for a prototype means a new instance.

Java
@Service
class ReportService {
private final ObjectProvider<ReportBuilder> builders;
ReportService(ObjectProvider<ReportBuilder> builders) { this.builders = builders; }
Report daily(List<String> rows) {
ReportBuilder builder = builders.getObject(); // new builder for this report
rows.forEach(builder::add);
return builder.build();
}
}
service.daily(List.of("a", "b")); // Report[rows=[a, b]]
service.daily(List.of("c")); // Report[rows=[c]]

This is the fix I would choose. The constructor makes it clear that the service creates builders on demand, the builder is a local variable so concurrent calls cannot share it, and a unit test can pass a simple ObjectProvider that returns new ReportBuilder().

4Fix 2: @Lookup or jakarta.inject.Provider

@Lookup method injection asks Spring to override a method so that it returns a bean from the container. We declare a method returning ReportBuilder, annotate it, and Spring generates a CGLIB subclass of ReportService at runtime whose version of the method calls getBean() each time.

Java
@Service
class ReportService {
@Lookup
protected ReportBuilder newBuilder() { return null; } // body replaced by Spring
Report daily(List<String> rows) {
ReportBuilder builder = newBuilder(); // new instance each call
rows.forEach(builder::add);
return builder.build();
}
}
// Standard alternative, not Spring-specific:
// ReportService(jakarta.inject.Provider<ReportBuilder> builders) { ... builders.get() ... }

@Lookup works, but it relies on subclassing, so ReportService cannot be final and the method cannot be private, and it does not work for beans created by @Bean factory methods. jakarta.inject.Provider works like ObjectProvider with a get() method and keeps the class free of Spring imports, provided the jakarta.inject API is on the classpath.

5Why a Prototype Scoped Proxy Is a Trap Here

It is tempting to add proxyMode = ScopedProxyMode.TARGET_CLASS to the prototype and keep the @Autowired field. The service then holds a proxy, and every method call on the proxy asks the container for the target. For a prototype, that means a new instance on every single method call, not per report.

Java
@Component
@Scope(value = "prototype", proxyMode = ScopedProxyMode.TARGET_CLASS)
class ReportBuilder { /* as before */ }
builder.add("a"); // instance #1 gets "a", then is forgotten
builder.add("b"); // instance #2 gets "b"
builder.build(); // instance #3: Report[rows=[]]

Notice that the report is empty, because each call went to a different builder. A stateful object needs the same instance across several calls, so the proxy breaks it in a new way. Prototype scoped proxies make sense only for objects where every call is independent, which is rare.

6Follow-up questions

Would making ReportService prototype-scoped fix it?

Only if everything that uses ReportService also asks the container for a new one each time, which pushes the same problem one level up. Usually ReportService is injected into a controller, which is a singleton, so it would also be injected once. The right fix is at the point where a new instance is needed.

Is there a simpler design without any container lookup?

Often, yes. If ReportBuilder needs no injected dependencies, the service can simply call new ReportBuilder() inside daily(). If it needs a few dependencies, a singleton factory with a create() method keeps things explicit. ObjectProvider is the best choice when the builder needs container features such as proxies or many dependencies.

How would a test have caught this bug?

A test that builds two reports in a row and checks the second contains only its own rows catches it immediately. A concurrency test that builds many reports in parallel catches the shared-state problem in web use. Single-call tests, which are the most common, never see it.

7Conclusion

In this answer, we saw that injecting a prototype into a singleton gives one instance for the singleton's whole life, because injection, and therefore the prototype request, happens only once at startup. The builder then carries data from one report to the next. We fixed it by asking for a new instance per report through ObjectProvider.getObject(), @Lookup method injection or jakarta.inject.Provider, and saw that a prototype scoped proxy is the wrong fix because it creates a new instance on every method call.

What to say in the interviewAbout 80 seconds aloud

Open with

The prototype scope works, but it only acts when the container is asked for the bean, and here it is asked exactly once. When Spring creates the ReportService singleton, it resolves the @Autowired field, gets one fresh ReportBuilder and stores it in the field.

Then, if they want more

From then on, every report uses that same builder, so rows from the previous report are still in its list, and concurrent requests would even share it. The fix is to ask the container for a builder at the moment we need one.

My preferred option is to inject ObjectProvider<ReportBuilder> through the constructor and call getObject() at the start of each report, keeping the builder in a local variable.

Alternatives are a @Lookup method, where Spring generates a subclass that overrides the method to return a new bean on each call, which needs a non-final class and does not work for @Bean factory methods, or jakarta.inject.Provider with get(), which avoids Spring imports.

I would not use a prototype scoped proxy, because it creates a new instance on every method call, so add, add and build would hit three different builders and the report would be empty. If the builder needs no dependencies at all, plain new is simplest.

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 singleton service injects a prototype-scoped ReportBuilder with @Autowired, expecting...": https://scrivio-interview-library.netlify.app/spring-boot/bean-scopes/db2987d270/