Scope Of Spring Bean

Snehatiwari
2 min readJun 25, 2021

--

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.

  1. 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.
  2. 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.
  3. 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.
  4. Session: when we declare scope Session it means a single bean definition to the lifecycle of a Http Session .Only valid for ApplicationContext.
  5. 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

HelloWord.java

this is the main class where we call object

client.java

this is the Xml file where bean is created

Application2.xml

Output:

I hope it will be helpful…

Thank You

Sneha Tiwari

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response