Quick revision
The short answer
When a bean is declared with a scoped proxy, Spring registers the real bean definition under a hidden name, scopedTarget.<name>, and registers a proxy under the original name. The proxy is a CGLIB subclass in TARGET_CLASS mode or a JDK interface proxy in INTERFACES mode, and on every method call it asks the scope for the current target, which creates it if needed, then forwards the call. The costs are a lookup on every call, confusing behaviour for methods like toString, equals and hashCode, and CGLIB's inability to proxy final classes or intercept final methods.
What to remember
In this answer, we saw that a scoped proxy is built from two bean definitions: the real bean under scopedTarget.<name>, not autowirable, and a singleton ScopedProxyFactoryBean under the original name that produces a CGLIB or JDK proxy. On each call, the proxy asks the bean factory, which asks the Scope, for the current target, creating it if needed. The costs are a lookup per call, surprising behaviour for toString, equals and hashCode and for use on other threads, and CGLIB's limits with final classes and silently skipped final methods.
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.
When a bean is declared with a scoped proxy, Spring registers the real bean definition under a hidden name, scopedTarget.<name>, and registers a proxy under the original name. The proxy is a CGLIB subclass in TARGET_CLASS mode or a JDK interface proxy in INTERFACES mode, and on every method call it asks the scope for the current target, which creates it if needed, then forwards the call. The costs are a lookup on every call, confusing behaviour for methods like toString, equals and hashCode, and CGLIB's inability to proxy final classes or intercept final methods.
Why this question is asked
Scoped proxies are what make request, session and custom scopes usable from singletons, and they show up in stack traces, in debugger views and in surprising bugs. Interviewers ask how they work inside because the answer connects several container features: bean definitions, factory beans, AOP proxies and scopes. In this answer, we will see how the proxy and target are registered, what kind of proxy is created, what happens on each method call, and then the costs and traps that follow from this design.
1How the Proxy and Target Are Registered
A bean definition is the container's recipe for a bean. When Spring finds a bean with a scoped proxy mode, for example @RequestScope or @Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS), it does not register one definition but two. A helper, ScopedProxyUtils, renames the original definition to scopedTarget.cart and marks it as not a candidate for autowiring, so nobody injects the real bean by type by accident. Under the original name, cart, it registers a new definition for a ScopedProxyFactoryBean, a factory bean that produces the proxy and knows the target's name.
The proxy definition is an ordinary singleton. That is why a singleton can inject it at startup: the proxy itself has no scope, only the target behind it does.
| Bean name | What it is | Scope | Autowire candidate |
|---|---|---|---|
| cart | ScopedProxyFactoryBean -> proxy | singleton | yes |
scopedTarget.cart | the real Cart definition | session | no |
Notice the proxy also implements ScopedObject, an interface that gives access to the current target (getTargetObject()) and lets us remove it from its scope (removeFromScope()).
2CGLIB or JDK Proxy
In TARGET_CLASS mode, the proxy is a CGLIB subclass of the bean's class, generated at runtime. It can be injected wherever the class is expected. Spring creates the proxy instance with Objenesis, a library that allocates an object without calling its constructor, so the proxy's own fields are never initialised; it never uses them, because every intercepted call goes to the target.
In INTERFACES mode, the proxy is a JDK dynamic proxy that implements the interfaces of the bean class. It can only be injected as one of those interfaces, never as the concrete class, but it does not need subclassing, so the bean class can be final.
3What Happens on Each Method Call
The proxy is backed by a target source, an object that tells the proxy which target to use for each call. For scoped proxies it is a simple bean target source, which on every call asks the bean factory for scopedTarget.cart. The bean factory sees that this definition has session scope and calls the registered Scope object's get(name, objectFactory). The session scope looks for an attribute named scopedTarget.cart in the current session; if it is there it returns it, and if not it calls the object factory to create a new Cart, stores it in the session and returns it. The proxy then invokes the method on that target.
7 messages in order. Play them, or step through them one at a time.
Every call repeats the lookup; nothing is cached in the proxy.
4The Costs of This Design
The first cost is a lookup on every call: a bean factory lookup, a scope lookup through a ThreadLocal-bound request, and a map lookup in the request or session attributes. Each is cheap, but in a loop calling the proxy a million times, it adds up, so it is better to fetch the value once into a local variable.
The second cost is confusing behaviour. Because every call goes to whichever target is current, the same proxy object stands for different targets at different times. Calling toString() on it, for example in a log statement or when a debugger shows the field, goes to the current target and fails with ScopeNotActiveException if no request is active. equals() and hashCode() may be handled by the proxy itself rather than the target, depending on the proxy type and version, so comparing a proxy with a target or putting proxies in a HashSet gives results that surprise people; check your version before relying on either. Storing the proxy somewhere and using it later on another thread also fails, because the current target depends on the thread.
5Final Classes and Final Methods
CGLIB works by subclassing and overriding, so it cannot proxy a final class: startup fails with an error saying the class cannot be subclassed. A final method is worse, because it fails silently. The subclass cannot override it, so a call to a final method runs on the proxy instance itself, which was created without running the constructor, and its fields are null or zero.
Kotlin classes are final by default, which is why Kotlin projects use the all-open compiler plugin for Spring. The INTERFACES proxy mode avoids both problems, at the cost of requiring an interface.
6Follow-up questions
Why is the target bean not an autowire candidate?
If it were, injection by type would find two beans of type Cart, the proxy and the target, and fail with an ambiguity error. Worse, if a singleton received the real target, it would keep one session's instance for ever. Marking the target as not autowirable makes injection by type always pick the proxy.
How can code get the real target object behind a scoped proxy?
Cast the proxy to ScopedObject and call getTargetObject(), which performs the same scope lookup and returns the current instance. This is useful for serialisation or for passing the real object to code that must not see a proxy. It still needs the scope to be active.
Do scoped proxies work with AOT and native images?
Yes, Spring's AOT processing handles scoped proxies and generates the proxy classes at build time, since a native image cannot generate classes at runtime. The same final class and final method limits apply. Check your version's documentation for any remaining restrictions.
7Conclusion
In this answer, we saw that a scoped proxy is built from two bean definitions: the real bean under scopedTarget.<name>, not autowirable, and a singleton ScopedProxyFactoryBean under the original name that produces a CGLIB or JDK proxy. On each call, the proxy asks the bean factory, which asks the Scope, for the current target, creating it if needed. The costs are a lookup per call, surprising behaviour for toString, equals and hashCode and for use on other threads, and CGLIB's limits with final classes and silently skipped final methods.
Open with
When a bean has a scoped proxy mode, Spring splits it into two bean definitions. The real definition is renamed to scopedTarget plus the bean name and marked as not an autowire candidate, and under the original name Spring registers a ScopedProxyFactoryBean, which is a singleton that produces the proxy.
Then, if they want more
With TARGET_CLASS the proxy is a CGLIB subclass of the bean class, created without running the constructor; with INTERFACES it is a JDK dynamic proxy that implements the bean's interfaces. The proxy holds no state.
On every method call, its target source asks the bean factory for the scopedTarget bean, the factory calls the Scope's get method, and the scope, say the request scope, looks in the current request's attributes and creates the instance through an object factory if it is missing. Then the call is forwarded.
The proxy also implements ScopedObject, so you can get the current target.
The costs: a lookup on every call, so avoid calling it in tight loops; confusing behaviour because the same proxy means different targets, so toString in logs fails outside a scope and equals or hashCode may not behave like the target's; and CGLIB cannot proxy final classes, while final methods are silently not intercepted and run on the empty proxy instance.
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 "How does a scoped proxy work inside, and what are its costs?": https://scrivio-interview-library.netlify.app/spring-boot/bean-scopes/4b36ff2b91/