Monday , November 20 2017
Home / Spring / Types of Spring Bean Scopes Example

Types of Spring Bean Scopes Example

In this tutorials, I am going to show you different types of Spring Bean Scopes with simple examples.

Spring Bean Scopes :

In spring we have 5 different types of bean scopes. We can define the bean scope in spring configuration file using scope attribute in <bean> tag or we can also use the @scope annotation on top of the class.

Types of Spring Bean Scopes :

1) singleton: It returns a single bean instance per Spring IoC container.

2) prototype: It returns a new bean instance for 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 entire HTTP session.

5) global session: global session scope is equal as session scope on portlet-based web applications.

Spring Bean Scopes using xml:

<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 spring bean scopes and declarations.

Note : If we don’t mention any scope explicitly, the default scope for a bean is singleton. It is a commonly asked spring interview question.

Spring Bean Scope using annotation :

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 🙂

About chandrashekhar

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Recommended

Spring Bean Autowire ByName Example

In this tutorial, we are going to learn about Spring bean autowire byName. Wiring in …

Leave a Reply

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