This project explores how Spring manages the lifecycle and number of instances for beans using the scope attribute.
| Feature | Singleton (Default) | Prototype |
|---|---|---|
| Instance Count | Only one instance per Spring Container. | A new instance every time the bean is requested. |
| Object Reference | obj1 == obj2 is True. |
obj1 == obj2 is False. |
| Use Case | Stateless beans (Services, Daemons). | Stateful beans (User sessions, multi-threaded tasks). |
| Creation | Created during container startup (Eager). | Created only when requested (Lazy). |
org.spring.bean.scopes.singleton: Demonstrates shared object state.org.spring.bean.scopes.prototype: Demonstrates independent object states.
By printing the object address in the Client class, we observe:
- Singleton:
Address of obj1==Address of obj2 - Prototype:
Address of obj1!=Address of obj2
While this project uses XML, you can achieve the same result in modern Spring using:
@Component
@Scope("prototype")
public class HelloWorld { ... }