Quick revision
The short answer
We declare a method that returns the prototype type, for example @Lookup protected ReportBuilder newBuilder() { return null; }, and call it whenever we need a new instance. Spring generates a CGLIB subclass of our bean that overrides this method to fetch the bean from the container, so each call returns a new prototype. Its limits are that the class must not be final and the method must be overridable, it does not work on beans created by @Bean factory methods, and ObjectProvider is usually simpler and more explicit.
What to remember
In this answer, we wrote a @Lookup method that returns a new prototype on each call, and saw that Spring implements it by recording a lookup override and generating a CGLIB subclass that calls getBean() from the overridden method. Its limits follow from that mechanism: the class must not be final, the method must be overridable, and it does not work when the bean comes from a @Bean factory method or new. ObjectProvider does the same job explicitly, without subclassing, and is usually the better choice.
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.
We declare a method that returns the prototype type, for example @Lookup protected ReportBuilder newBuilder() { return null; }, and call it whenever we need a new instance. Spring generates a CGLIB subclass of our bean that overrides this method to fetch the bean from the container, so each call returns a new prototype. Its limits are that the class must not be final and the method must be overridable, it does not work on beans created by @Bean factory methods, and ObjectProvider is usually simpler and more explicit.
Why this question is asked
Method injection is one of Spring's older and less known features, and @Lookup is its annotation form. It solves the classic problem of a singleton that needs a fresh prototype each time, and interviewers ask for it to see whether we know how Spring can change our class's behaviour through generated subclasses. In this answer, we will recap the problem it solves, write a working example, look at what Spring generates behind the scenes, go through the limits one by one, and compare it with ObjectProvider.
1A Quick Recap of the Problem
A singleton bean is created once, and its dependencies are injected once. If one of those dependencies is a prototype, the singleton receives a single instance and keeps it. When the singleton needs a new prototype instance each time, for example a stateful builder per report, it must ask the container at the moment it needs one. @Lookup lets it do that through an ordinary method call, without referring to the container in its code.
2Writing a @Lookup Method
We write a method whose return type is the bean we want and annotate it with @Lookup. The body does not matter, because Spring will replace it; returning null is the convention. The method can also be abstract, in which case the class is abstract, and component scanning still picks it up because it has lookup methods.
Notice the last line. The ReportService bean in the container is not exactly our class but a generated subclass. That subclass is how the trick works. We can also name the bean explicitly with @Lookup("reportBuilder"); without a name, Spring looks the bean up by the method's return type, and if the method has parameters, they are passed as constructor arguments to the prototype.
3What Spring Generates Behind the Scenes
When Spring reads the ReportService class, it records the @Lookup method as a lookup override in the bean definition. When it is time to create the bean, the instantiation strategy sees that the definition has method overrides and, instead of calling new ReportService(), it uses CGLIB to generate a subclass. In that subclass, newBuilder() is overridden with a method interceptor that calls the bean factory's getBean() for ReportBuilder. Spring then creates an instance of the subclass, injects its dependencies and initialises it as usual.
- Scan ReportServicefind
@LookupnewBuilder() - Record a lookup overridein the bean definition
- Definition has method overrides?YesGenerate CGLIB subclassNoCall the constructor normally
- Instantiate the subclassthen inject and initialise
- Each newBuilder() call->
beanFactory.getBean(ReportBuilder)
5 steps in order. Play them, or step through them one at a time.
The override exists only because Spring creates the object itself.
4The Limits of @Lookup
Because the mechanism is subclassing, the class must not be final, and the lookup method must be one a subclass can override: not private, not final and not static. Kotlin classes are final by default, so they need the all-open plugin or the open keyword. A mistake here does not always fail loudly; if the method cannot be overridden, our own body runs and returns null, which shows up later as a NullPointerException.
The biggest limit is that lookup methods only work when the container instantiates the class itself. If the bean comes from a @Bean factory method, our code calls new ReportService() inside that method, so the container never gets the chance to substitute a generated subclass, and the @Lookup method simply runs its own body. The Spring documentation states this limitation explicitly. The same applies to objects created with new anywhere else.
There are softer costs too. The behaviour is hidden: a reader sees a method returning null and must know that Spring rewrites it. Unit tests that create the class with new get the null as well, so tests have to subclass it manually or use a Spring context. And generated subclasses add a little to startup work and to the complexity of AOT processing.
5Why ObjectProvider Is Usually Simpler
ObjectProvider<T> does the same job with no generated subclass. We inject it through the constructor and call getObject() whenever we need a new instance. It works for beans from component scanning and from @Bean methods alike, the dependency is visible in the constructor, and a unit test can pass a lambda-based provider. It also offers getIfAvailable() and getIfUnique() for optional beans, and stream() to get all matching beans.
@Lookup still has a place in code that must not depend on Spring's API in its signatures, or in abstract template classes where subclasses provide the rest. For most application code, ObjectProvider is the clearer choice.
6Follow-up questions
Can a @Lookup method take arguments?
Yes. If the method has parameters, Spring passes them as explicit arguments to getBean(), which uses them as constructor arguments for the prototype. For example, @Lookup ReportBuilder newBuilder(Locale locale) creates a builder with that locale. The matching happens at runtime, so mismatched arguments fail only when the method is called.
Does @Lookup work with singleton target beans?
It does, but then each call returns the same singleton, which gives no benefit over plain injection except lazy resolution. Lookup methods are meant for prototypes or other scoped beans, where every lookup may return a different instance.
Does @Lookup work in a native image?
Spring's AOT processing can generate the needed subclass at build time, since a native image cannot generate classes at runtime. Check your version's documentation for current support; ObjectProvider avoids the question entirely because it needs no generated class.
7Conclusion
In this answer, we wrote a @Lookup method that returns a new prototype on each call, and saw that Spring implements it by recording a lookup override and generating a CGLIB subclass that calls getBean() from the overridden method. Its limits follow from that mechanism: the class must not be final, the method must be overridable, and it does not work when the bean comes from a @Bean factory method or new. ObjectProvider does the same job explicitly, without subclassing, and is usually the better choice.
Open with
With @Lookup, I declare a method in the singleton that returns the prototype type, for example a protected ReportBuilder newBuilder() that just returns null, and annotate it. Then in my business method I call newBuilder() every time I need a fresh builder.
Then, if they want more
Spring records the annotated method as a lookup override in the bean definition, and when it creates the bean, it does not call my constructor directly but generates a CGLIB subclass that overrides newBuilder() to call getBean on the bean factory.
So each call returns a new prototype instance, and if the method has parameters they are passed as constructor arguments. The limits come from that subclassing. The class cannot be final and the method cannot be private, final or static, which matters for Kotlin.
It only works when the container instantiates the class itself, so it does not work for beans returned from @Bean factory methods, where my own code calls new and my null-returning body runs. It is also hidden behaviour, and unit tests that use new get null.
That is why I usually prefer ObjectProvider<ReportBuilder> in the constructor and getObject() per use: same result, explicit, works with @Bean methods, and easy to test.
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.
- Official documentation
- Spring Framework reference, Core technologies
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 "Show how @Lookup method injection works for getting a new prototype from a singleton....": https://scrivio-interview-library.netlify.app/spring-boot/bean-scopes/12c706d35e/