Scrivio Interview library
DesignIntermediate6 min read · updated 12 November 2026

When would you choose prototype scope, and when is a plain 'new' or a factory method better?

Prototype scope is worth it when each user needs a new stateful object that still needs things only the container can provide, such as injected dependencies, configuration or proxies for @Transactional or @Async. If the object needs none of that, creating it with new or a small factory method is clearer and faster. We should also remember that a prototype injected once into a singleton behaves like a singleton, and that heavy use of prototypes often means state should be passed as method parameters instead.

Why this question is asked

Every Spring developer eventually needs "a new object each time" and has to decide how to get it. The choices look similar but differ in clarity, testability and cost. Interviewers ask this design question to see whether we treat the container as a tool for specific needs rather than the default way to create every object. In this answer, we will look at what prototype scope actually gives us, when new or a factory is the better choice, the common trap with singletons, and what heavy prototype use tells us about a design.

1What Prototype Scope Actually Gives Us

A prototype bean is a new instance created by the container on each request. Compared with new, the container adds three things. It injects dependencies, so the object can receive repositories, clients and configuration without the caller passing them. It applies BeanPostProcessors, which means the new object can be wrapped in proxies, so @Transactional, @Async, @Validated or a custom aspect work on it. And it runs initialisation callbacks such as @PostConstruct.

Those benefits have costs. Creating a prototype goes through the container's creation path, resolving dependencies and running every post-processor, which is much slower than a constructor call; it matters when we create thousands of objects per second. The object's dependencies become invisible at the call site, because the caller asks the container rather than passing them. And the caller must know the object is a prototype, otherwise it may keep it too long or forget to clean it up.

2When new or a Factory Method Is Better

Most objects that need to be new each time are values or short-lived helpers: a builder, a request object, a parser state, a DTO. They need no injected services, so the container adds nothing but indirection. Creating them with new keeps the code obvious and the tests trivial.

Java
// A plain helper: no container services needed, so just use new
var csv = new CsvLineBuilder(';');
csv.add("id").add("total");
System.out.println(csv.build()); // id;total
// Needs dependencies, but no proxies: a singleton factory keeps them explicit
@Component
class ReportBuilderFactory {
private final TemplateEngine templates;
private final Clock clock;
ReportBuilderFactory(TemplateEngine t, Clock c) { templates = t; clock = c; }
ReportBuilder create(Locale locale) { // runtime arguments are easy to pass
return new ReportBuilder(templates, clock, locale);
}
}

Notice the factory version. The factory is an ordinary singleton that holds the shared dependencies, and its create() method builds the stateful object with new, passing both the dependencies and runtime arguments such as the locale. This pattern covers most cases people reach for prototypes for. It is fast, easy to read, and easy to test, because a test can call new ReportBuilder(...) directly.

3When Prototype Is the Right Choice

Prototype earns its place when the new object needs container features that a factory cannot easily provide, mainly proxies. If each import run needs its own stateful ImportSession with a @Transactional method, a factory using new would give an object without the transaction proxy. A prototype bean obtained through ObjectProvider gets the proxy automatically.

It is also handy when the object's dependencies vary by environment or profile and we do not want a factory that knows them all, or in framework-like code that creates user-supplied components by type. In those cases, ObjectProvider<T> with getObject(), or getObject(args) to pass constructor arguments, makes the request for a new instance explicit at the call site.

Step throughChoosing how to create a new object each time
  1. Needs injected dependencies?
    YesNext questionNoUse new
  2. Needs proxies, such as @Transactional?
    YesPrototype via ObjectProviderNoNext question
  3. Needs runtime arguments too?
    YesSingleton factory with newNoSingleton factory or prototype
  4. If the state lasts only one callpass it as parameters to a singleton instead

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

Most cases end at new or a factory; prototypes are for container features.

4The Trap: Prototypes Inside Singletons

The most common surprise is that injecting a prototype into a singleton with @Autowired gives exactly one instance. The singleton is created once, its dependencies are injected once, and the prototype it received is kept for its whole life. So the code looks like it gets a new object each time, but it shares one, often with state left over from earlier calls.

Java
@Service
class Importer {
@Autowired ImportSession session; // prototype, but injected once
void run(File f) { session.load(f); } // every run shares the same session
}
@Service
class FixedImporter {
private final ObjectProvider<ImportSession> sessions;
FixedImporter(ObjectProvider<ImportSession> s) { sessions = s; }
void run(File f) { sessions.getObject().load(f); } // new session per run
}

5What Heavy Prototype Use Tells Us

If a codebase has many prototype beans, it is worth asking why so many objects carry state. Often the state belongs to one operation and could simply be passed along: a method that takes the import file, the user and the options as parameters, or returns a result object, needs no per-call instance at all. A stateless singleton with explicit parameters is easier to test, cheaper to run and safe across threads.

So as a rule of thumb, we prefer stateless singletons with parameters first, new or a factory for stateful helpers second, and prototype beans only when the new object needs container features such as proxies.

6Follow-up questions

Is ObjectProvider.getObject(args) a good way to pass runtime arguments to a prototype?

It works: Spring matches the arguments to a constructor of the prototype and injects nothing else for those parameters. But the matching is by position and type at runtime, so a mistake is only found when the code runs. A singleton factory with a typed create() method checks the arguments at compile time, which is why it is usually clearer.

Can a factory-created object still use @Transactional?

Not if it is created with new, because no proxy wraps it. The options are to move the transactional method into a singleton service that the object calls, to use the TransactionTemplate programmatically inside the object, or to obtain the object as a prototype bean so that it gets a proxy.

Does creating prototypes affect performance?

Creating a prototype goes through dependency resolution and every BeanPostProcessor, and possibly proxy creation, so it costs much more than a constructor call. It is fine for objects created a few times per request, but in tight loops creating millions of objects a factory or new is better.

7Conclusion

In this answer, we saw that prototype scope adds dependency injection, post-processors and proxies, and initialisation callbacks to object creation, at the cost of speed and visibility. If the object needs no container services, new or a singleton factory that passes dependencies and runtime arguments is clearer. Prototypes injected once into singletons behave like singletons, so we use ObjectProvider to get a new instance each time, and many prototypes in a design often mean state should be passed as parameters instead.

What to say in the interviewAbout 70 seconds aloud

Open with

I choose prototype scope only when each user needs a new stateful object that also needs something only the container can give: injected dependencies plus proxies, like @Transactional or @Async, or post-processing.

Then, if they want more

In that case I get it through ObjectProvider.getObject at the point of use, so it is clear that a new instance is being created. If the object needs nothing from the container, such as a builder or a value object, plain new is clearer and much faster.

If it needs dependencies but no proxies, I prefer a singleton factory bean that holds the shared dependencies and has a typed create method, so I can also pass runtime arguments and tests stay simple.

I keep the classic trap in mind: if a singleton @Autowires a prototype, injection happens once, so the singleton keeps one instance and state leaks between calls.

And if I see many prototypes in a codebase, I ask whether the state really needs an object at all, because often it can be passed as method parameters to a stateless singleton, which is easier to test and safe across threads.

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 "When would you choose prototype scope, and when is a plain 'new' or a factory method...": https://scrivio-interview-library.netlify.app/spring-boot/bean-scopes/40eb789fdc/