Types of Spring Bean Scopes Example - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, we will see the different types of Spring Bean Scopes.
In Spring there is a possibility to define a scope of a bean, based on your requirement you can make use of it. We can define the scope of a bean as part of the configuration details, if your application is based on XML configuration, then you can define the scope using scope attribute in <bean> tag or if you are using annotation-based configuration then you can use @scope annotation.
Basically Spring we have 5 different types of bean scopes, which are described below.
1) singleton: It returns a single bean instance per Spring IoC container.
2) prototype: It returns a new bean instance each time when requested to create a bean.
3) request: It returns a single instance for every HTTP request.
4) session: It returns a single instance for the entire HTTP session.
5) global session: global session scope is equal to session scope on portlet-based web applications.
<beans xmlns="http://www.springframework.org/schema/beans" =
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    <bean id="company" scope="singleton"
        class="com.onlinetutorialspoint.springcoreapplication.Company">
        <bean id="employee" scope="prototype"
            class="com.onlinetutorialspoint.springcoreapplication.Employee">
            <bean id="student" scope="request"
                class="com.onlinetutorialspoint.springcoreapplication.Student">
                <bean id="customer" scope="session"
                    class="com.onlinetutorialspoint.springcoreapplication.Customer">
                    <bean id="item" scope="globalsession"
                        class="com.onlinetutorialspoint.springcoreapplication.Item">
                    </bean>
                </bean>
            </bean>
        </bean>
    </bean>
</beans>
On the above configuration, we can see all types of spring bean scopes and declarations.
Note: If we don’t mention any scope explicitly, the default scope for a bean is a singleton. It is a commonly asked spring interview question.
We can define a bean scope in spring using @scope annotation.
@Component
@Scope("prototype")
public class Customer {

    private int id;

    public Customer() {
        System.out.println(" id is:" + id);
    }

    public String getId() {
        return id;
    }
}
We can define all types of bean scopes by passing the parameter to @scope annotation.
Happy Learning 🙂

Spring Bean Autowire By Constructor Example
Spring Bean Autowire ByName Example
Spring Bean Autowire ByType Example
Spring JdbcTemplate CRUD Application
@Qualifier annotation example in Spring
Spring Java Configuration Example
@Autowired,@Qualifier,@Value annotations in Spring
Spring Collection Dependency List Example
Spring Collection Map Dependency Example
BeanFactory Example in Spring
spring expression language example
@Component,@Service,@Repository,@Controller in spring
Spring MVC HelloWorld
Spring Boot Lazy Loading Beans Example
Spring MVC Login Form Example Tutorials

Spring Bean Autowire By Constructor Example
Spring Bean Autowire ByName Example
Spring Bean Autowire ByType Example
Spring JdbcTemplate CRUD Application
@Qualifier annotation example in Spring
Spring Java Configuration Example
@Autowired,@Qualifier,@Value annotations in Spring
Spring Collection Dependency List Example
Spring Collection Map Dependency Example
BeanFactory Example in Spring
spring expression language example
@Component,@Service,@Repository,@Controller in spring
Spring MVC HelloWorld
Spring Boot Lazy Loading Beans Example
Spring MVC Login Form Example Tutorials