What are the Bean Scopes in Spring framework

In this post, we will learn, the different types of Bean scopes in Spring framework. Bean instance created by the Spring container can be in one of the following scopes:

  1. Singleton
  2. Prototype
  3. Request
  4. Session
  5. Global-session

Usage: <bean id=” ” class=” “ scope=” ”/>

1) singleton:

  • When bean scope is singleton then the only instance will be created for that and the same instance will be returned when you call getBean() method.
  • Singleton is the default scope in the Application Context container.
  • When a scope is single-ton then default loading type is aggressive loading.

2) prototype:

  • When bean scope is prototype then every time a name instance will be created for that bean when you call getBean() method.
  • When a scope is a prototype then default loading type is lazy loading.

3) request:

  • Request scope is equaled to HttpSession in the web application

4) session:

  • Session scope is equaled to HttpSession in the web application.

5) global- session:

  • The global-session scope is equaled to session in the portlet bases web application

Bean loading types

  • Bean configured in the spring context.xml can be loaded into two ways.
  1. Aggressive loading or Eager Loading
  2. Lazy loading

usage:

<bean id=” ” class=” “ scope=” ” lazy-init=” ”/>

1) Aggressive loading or Eager Loading:

In the case of aggressive loading, all the beans will be loaded, instantiated and initialized by the container at the container start-up

<bean id=” ” class =” ” scope=” ” lazy-init=” “ false”/>

2) Lazy Loading
In the case of lazy loading, all the Beans will be loaded, instantiated and initialized when you or container try to use them by calling geyBean() method.

<bean id=” ” class=” “ scope=” ” lazy-init=”true ”/>

Some of the important interview questions and answers are listed below:

Q1 How many bean scopes are there?
Ans: 5

Q2 What is the default bean scope?
Ans: Singleton

Q3 What is the difference between singleton and prototype?
Ans: Refer to the article

Q4 How many bean loading types are there?
Ans: 2

Q5 What is the difference between Lazy loading and Aggressive Loading?
Ans: In Aggressive Loading: In the case of aggressive loading, all the beans will be loaded, instantiated and initialized by the container at the container start-up

In Lazy Loading: In the case of lazy loading, all the Beans will be loaded, instantiated and initialized when you or container try to use them by calling geyBean() method.

Q6 What will happen with the following?
<bean id=”ao” class= “A”>
……
</bean>
<bean id=”bo” class= “B”>
……
</bean>
<bean id=”hello” class= “Hello”>
……
</bean>
Ans: All the three beans will be loaded at the container start-up and all are single-tons. Order of the loading : A-B-Hello

Q7 What will happen with the following?
<bean id=”ao” class= “A” lazy-init=”true”>
……
</bean>
<bean id=”bo” class= “B” lazy-init=”true”>
……
</bean>
<bean id=”hello” class= “Hello” lazy-init=”true”>
……
</bean>
Ans: No bean will be loaded at the container start-up
All are singleton
Loads when you call getBean() method.
Order of loading: A- B-Hello

Q8 What will happen with the following?
<bean id=”ao” class= “A” lazy-init=”true”>
……
</bean>
<bean id=”bo” class= “B” lazy-init=”true”>
……
</bean>
<bean id=”hello” class= “Hello” lazy-init=”false”>
……
</bean>
Ans: one bean will be loaded at the container start-up
All are singleton

Q9 What will happen with the following?
<bean id=”ao” class= “A” lazy-init=”false”>
……
</bean>
<bean id=”bo” class= “B” lazy-init=”false”>
……
</bean>
<bean id=”hello” class= “Hello” lazy-init=”true”>
……
</bean>
Ans: Two bean will be loaded at the container start-up
All are singleton. Order of loading of beans: A-B

Q10 What will happen with the following?
<bean id=”ao” class= “A” lazy-init=”false” scope=”prototype”>
……
</bean>
<bean id=”bo” class= “B” lazy-init=”false” scope=”prototype”>
……
</bean>
<bean id=”hello” class= “Hello” lazy-init=”true” scope=”singleton”>
……
</bean>
Ans: No bean will be loaded at the container start-up
A, B are prototype
Hello is singleton.

Q11 What will happen with the following?
<bean id=”ao” class= “A” scope=”prototype”>
……
</bean>
<bean id=”bo” class= “B” scope=”prototype”>
……
</bean>
<bean id=”hello” class= “Hello” lscope=”singleton”>
……
</bean>
Ans: All 3 beans will be loaded at the container start-up
A and B are the prototype. Order of loading: B-hello-A

Conclusion:
In this article we learned, the bean scopes and the types of bean loading in the spring framework with their proper definition. we have seen some of the important interview questions regarding the topic. Please feel free to drop a comment in the comment section, if you have doubt about the topic or you want to share more information about the topic. Happy Learning!

Leave a Reply

Your email address will not be published. Required fields are marked *