Scope Of Spring Bean
so lets start with the scope of bean . ……
Bean scopes refers to the lifecycle of bean it means when the object of Bean will be instantiated,how long does that object live,and how many objects will be created .
In simple word scope just like boundary for the bean.
In Spring Framework five scopes are present for a bean. And we can use three of them only in context of web-aware Spring ApplicationContext. And rest of two availabe for both IOC conatiner and MVC container.
- Singleton: when we declare scope singleton it means only one object will be created for a single bean and the same object will be shared for each request made for that bean.
- Prototype: when we declare scope prototype so for a single bean every time a new object will be created at every time a request is made for that bean.
- Request: when we declare scope Request for a single bean every time a new object will be created at every time a Http request is made for that bean.Only valid for ApplicationContext.
- Session: when we declare scope Session it means a single bean definition to the lifecycle of a Http Session .Only valid for ApplicationContext.
- Global-Session:when we declare scope Session it means a single bean definition to the lifecycle of a global Http Session .Only valid for ApplicationContext.
lets take a example of singleton scope:
this is simple java bean class

this is the main class where we call object

this is the Xml file where bean is created

Output:

I hope it will be helpful…
Thank You
Sneha Tiwari