What this animation explains
A singleton is shared within a bean definition and context; prototype creation occurs on each lookup. Two variables may point to the same object or different objects, affecting mutable state.

MODULE 4 · TOPIC 8 · VISUAL SPRING FRAMEWORK LAB
A bean scope controls how instances are obtained and reused. Singleton is one managed instance per bean definition in a container; prototype creates a new instance for each lookup. Request, session, application and websocket scopes require appropriate web-aware infrastructure. The container runs lifecycle callbacks around initialization and destruction, but prototype destruction is the client’s responsibility. Scope choice changes identity and state sharing, not the class definition itself.
Browser-based Spring simulations. Change inputs and trace bean wiring, application layers, configuration and transaction behavior. No real Java, Spring or database operations are executed.
A singleton is shared within a bean definition and context; prototype creation occurs on each lookup. Two variables may point to the same object or different objects, affecting mutable state.
Why this example? Look up a counter twice, increment via reference A and inspect B.
Arrows show the actual route; blue identifies the current node. Green indicates visited operations; red nodes show errors or rejected paths.
Change inputs and step through the example. Code and output are illustrative; actual Spring logs and initialization order may differ. Use the worked examples in a real project.
A singleton is shared within a bean definition and context; prototype creation occurs on each lookup. Two variables may point to the same object or different objects, affecting mutable state.
Compare B after the increment.
In a servlet web application, request scope follows the current HTTP request and session scope follows the current HTTP session. Two requests in the same session share a session-scoped target but not a request-scoped target. Separate users have separate sessions.
Why this example? Trace user A twice and user B once under different scopes.
Arrows show the actual route; blue identifies the current node. Green indicates visited operations; red nodes show errors or rejected paths.
Change inputs and step through the example. Code and output are illustrative; actual Spring logs and initialization order may differ. Use the worked examples in a real project.
In a servlet web application, request scope follows the current HTTP request and session scope follows the current HTTP session. Two requests in the same session share a session-scoped target but not a request-scoped target. Separate users have separate sessions.
Inspect IDs and the associated request or session.
For a normal singleton, Spring instantiates the object, populates dependencies, invokes initialization callbacks, exposes the processed bean (possibly a proxy) and later invokes destruction callbacks when the context closes. Prototype initialization is supported, but destruction is not automatically managed.
Why this example? Compare callback traces for a singleton and a requested prototype.
Arrows show the actual route; blue identifies the current node. Green indicates visited operations; red nodes show errors or rejected paths.
Change inputs and step through the example. Code and output are illustrative; actual Spring logs and initialization order may differ. Use the worked examples in a real project.
For a normal singleton, Spring instantiates the object, populates dependencies, invokes initialization callbacks, exposes the processed bean (possibly a proxy) and later invokes destruction callbacks when the context closes. Prototype initialization is supported, but destruction is not automatically managed.
Close the context and compare cleanup behavior.
Eager singleton pre-instantiation discovers many configuration errors during context refresh. Lazy singleton creation shifts work to first access unless needed by another eager bean. Lazy does not mean prototype.
Why this example? Change initialization mode and observe when work is done.
Arrows show the actual route; blue identifies the current node. Green indicates visited operations; red nodes show errors or rejected paths.
Change inputs and step through the example. Code and output are illustrative; actual Spring logs and initialization order may differ. Use the worked examples in a real project.
Eager singleton pre-instantiation discovers many configuration errors during context refresh. Lazy singleton creation shifts work to first access unless needed by another eager bean. Lazy does not mean prototype.
Compare refresh and first lookup.
Injecting a prototype directly into a singleton happens once, at construction. A provider resolves a prototype on every getObject call. A scoped proxy is another option, with different method-call semantics.
Why this example? Compare a stored Job reference and a provider lookup.
Arrows show the actual route; blue identifies the current node. Green indicates visited operations; red nodes show errors or rejected paths.
Change inputs and step through the example. Code and output are illustrative; actual Spring logs and initialization order may differ. Use the worked examples in a real project.
Injecting a prototype directly into a singleton happens once, at construction. A provider resolves a prototype on every getObject call. A scoped proxy is another option, with different method-call semantics.
Observe the job IDs across two tasks.
A singleton may need a request-scoped dependency. A scoped proxy can be injected at startup and resolve the real target during an active request. Calling it without the required active request scope can still fail.
Why this example? Compare invoking the same proxy during a request and outside one.
Arrows show the actual route; blue identifies the current node. Green indicates visited operations; red nodes show errors or rejected paths.
Change inputs and step through the example. Code and output are illustrative; actual Spring logs and initialization order may differ. Use the worked examples in a real project.
A singleton may need a request-scoped dependency. A scoped proxy can be injected at startup and resolve the real target during an active request. Calling it without the required active request scope can still fail.
Notice the proxy identity remains fixed while target availability changes.
Refer to these APIs and settings while building a real Spring project.
| API / setting | Why and when to use it |
|---|---|
| @Scope("prototype") | Create a new bean per lookup |
| @Lazy | Defer eligible singleton initialization |
| @RequestScope | One scoped target per HTTP request |
| @SessionScope | One scoped target per HTTP session |
| @PostConstruct | Initialization callback (Jakarta annotation) |
| @PreDestroy | Destruction callback for managed lifecycle |
Follow these procedures with the Java 21 / Spring Framework 6.2.0 teaching project. Unless complete files are provided, the code is a focused snippet requiring the indicated imports, companion classes and configuration.
@Configuration class Config {
@Bean Job shared() { return new Job(); }
@Bean @Scope("prototype") Job fresh() { return new Job(); }
}
class Job {}
// In main, with ctx open:
System.out.println(ctx.getBean("shared") == ctx.getBean("shared"));
System.out.println(ctx.getBean("fresh") == ctx.getBean("fresh"));class ResourceBean {
@jakarta.annotation.PostConstruct
void init() { System.out.println("open resource"); }
@jakarta.annotation.PreDestroy
void cleanup() { System.out.println("close resource"); }
}Try answering these questions, then expand each answer and repeat the appropriate simulation.
B refers to a different object. Expected state: 0.
Client must manage prototype cleanup. Expected state: No automatic destroy.
Second lookup creates another prototype. Expected state: Job #2.
Create singleton and prototype Job beans, request each twice, compare references, then close the context and record callbacks.
Edit this practice scratchpad and copy it into your real Java project. The browser does not compile arbitrary Java.
Use stateless singleton services where possible. Request/session scoped beans require active scopes, and a scoped proxy/provider can bridge shorter lifetimes.