Spring Tutorial Home

  • Managing Database Transactions
  • Remoting in Spring
  • Working with the Web Layer

Spring Home

Java Resources

Community

Site

"http://www.w3.org/TR/html4/loose.dtd">









Bean scopes in Spring




In Spring user can control the scope of the objects created from a
particular bean definition. This can be achieved by the attribute "scope" in the <bean> tag. Following are the scopes available in the Spring.



  1. singleton:-Scopes the bean definition to a single instance per Spring container (default).

  2. prototype:-Allows a bean to be instantiated any number of times but bean is intialized once for a user.

  3. request:-Scopes a bean definition to an HTTP request. Only valid when used with a webcapable
    Spring context (such as with Spring MVC).

  4. session:-Scopes a bean definition to an HTTP session. Only valid when used with a webcapable
    Spring context (such as with Spring MVC).

  5. global-session:-Scopes a bean definition to a global HTTP session. Only valid when used in a
    portlet context.



Example for various scopes is a follows:-


Note:-The prototype,request,session and global-session examples will be covered in the Web application part. The below example will cover the Singleton example.







<?xml version="1.0" encoding="UTF-8"?>

<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="test" class="com.visualbuilder.beans.TestBean" />

    <bean id="disposableBean" class="com.visualbuilder.beans.DisposableInitBean" scope="singleton" />

    <bean id="collectionExample" class="com.visualbuilder.beans.CollectionInjectionBean" scope="singleton">

        <property name="products">

            <list>

                <value>CAR</value>

                <value>MOTORCYCLE</value>

                <value>SCOOTER</value>

            </list>

        </property>

        <property name="productSet">

            <set>

                <value>CAR</value>

                <value>MOTORCYCLE</value>

                <value>SCOOTER</value>

            </set>

        </property>


        <property name="prices">

            <map>

                <entry key="
CAR" value="100" />

                <entry key="
MOTORCYCLE" value="200" />

                <entry key="
SCOOTER" value="10" />

            </map>

        </property>

    </bean>  

    <bean id="
typeCollectionExample" class="com.visualbuilder.beans.TypeCollectionInjectionBean" scope="singleton">

        <property name="
products">

            <list>

                <value>
CAR</value>

                <value>
MOTORCYCLE</value>

                <value>
SCOOTER</value>

            </list>

        </property>

        <property name="prices">

            <map>

                <entry key="CAR" value="100" />

                <entry key="MOTORCYCLE" value="200" />

                <entry key="SCOOTER" value="10" />

            </map>

        </property>

    </bean>

    <bean id="
constructorInjection" class="com.visualbuilder.beans.ConstructorInjectionBean" scope="singleton">

        <constructor-arg type="
java.lang.String" value="John Smith" />

        <constructor-arg index="
1" value="25" />

        <constructor-arg value="
M" />

    </bean>


    <bean id="setterInjection" class="com.visualbuilder.beans.SetterInjectionBean" sscope="singleton" >

        <property name="
name" value="John Smith" />

        <property name="
age" value="25" />

        <property name="
sex" value="M" />

    </bean>

    <bean id="
wrapperBean" class="com.visualbuilder.beans.WrapperBean" scope="singleton" >

        <property name="
collectionInjectionBean">

            <ref bean="
collectionExample" />

        </property>

        <property name="
constructorInjectionBean">

            <ref bean="
constructorInjection" />

        </property>

        <property name="
disposableInitBean">

            <ref bean="
disposableBean" />

        </property>

        <property name="
setterInjectionBean">

            <ref bean="
setterInjection" />

        </property>

        <property name="
typeCollectionInjectionBean">

            <ref bean="
typeCollectionExample" />

        </property>

    </bean>

</beans>



ApplicationContextExample.java







package com.visualbuilder.factory;


import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;


import com.visualbuilder.beans.TestBean;


public class ApplicationContextExample {

    public static void main (String [] ar){

                ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"com/visualbuilder/xml/spring-beans.xml"});

                TestBean bean = (TestBean)context.getBean("test");

        System.out.println(bean.toString());

        }

}


Output:-


Jun 2, 2008 12:38:25 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh

INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc: display name [org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc]; startup date [Mon Jun 02 12:38:25 IST 2008]; root of context hierarchy

Jun 2, 2008 12:38:26 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

INFO: Loading XML bean definitions from class path resource [com/visualbuilder/xml/spring-beans.xml]

Jun 2, 2008 12:38:26 PM org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory

INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc]: org.springframework.beans.factory.support.DefaultListableBeanFactory@d6c16c

afterPropertiesSet method is called

Jun 2, 2008 12:38:26 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons

INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@d6c16c: defining beans [test,disposableBean,collectionExample,typeCollectionExample,constructorInjection,setterInjection,wrapperBean]; root of factory hierarchy

Bean is created

Bean has been called





                    

Copyright © 2013 VisualBuilder. All rights reserved