The largest Interview Solution Library on the web


« Previous | 2 | | 4 | Next »

Spring - Bean Scopes

  • When defining a in Spring, you have the option of declaring a scope for that bean.
  • For example, To force Spring to produce a new bean instance each time one is needed, you should declare the bean's scope attribute to be prototype.
  • Similar way if you want Spring to return the same bean instance each time one is needed, you should declare the bean's scope attribute to be singleton.
  • The Spring Framework supports following five scopes, three of which are available only if you use a web-aware ApplicationContext:


  • singleton
  • This scopes the bean definition to a single instance per Spring IoC container (default).   
  • prototype.
  • This scopes a single bean definition to have any number of object instances.   
  • request
  • This scopes a bean definition to an HTTP request. Only valid in the context of a web-aware Spring ApplicationContext.   
  • session
  • This scopes a bean definition to an HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.   
  • global-session
  • This scopes a bean definition to a global HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.   

    The Singleton scope:

       HelloWorld.java
    package com.jtc;
    public class HelloWorld {
    private String message;
    public void setMessage(String message){
    this.message = message;
    }
    public void getMessage(){
    System.out.println("Your Message : " + message);
    }
    }
    MainApp.java
    package com.jtc;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class MainApp {
    public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
    HelloWorld objectA = (HelloWorld) context.getBean("helloWorld");
    objectA.setMessage("I am object A");
    objectA.getMessage();
    HelloWorld objectB = (HelloWorld) context.getBean("helloWorld");
    objectB.getMessage();
    }
    }
    Beans.xml
    <?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-3.0.xsd">
       <bean id=
       "helloWorld" 
       class="com.jtc.HelloWorld"scope="singleton"
       >
           <property name
    	   ="message" value=
    	   "Hello World!"/>
       </bean>
    </beans>
    Add Below Libraries
    commons-logging-1.1.1
    spring-aop-4.1.6.RELEASE
    spring-aspects-4.1.6.RELEASE
    spring-beans-4.1.6.RELEASE
    spring-context-4.1.6.RELEASE
    spring-context-support-4.1.6.RELEASE
    spring-core-4.1.6.RELEASE
    spring-expression-4.1.6.RELEASE
    spring-instrument-4.1.6.RELEASE
    spring-instrument-tomcat-4.1.6.RELEASE
    spring-jdbc-4.1.6.RELEASE
    spring-jms-4.1.6.RELEASE
    spring-messaging-4.1.6.RELEASE
    spring-orm-4.1.6.RELEASE
    spring-oxm-4.1.6.RELEASE
    spring-test-4.1.6.RELEASE
    spring-tx-4.1.6.RELEASE
    spring-web-4.1.6.RELEASE
    spring-webmvc-4.1.6.RELEASE
    spring-webmvc-portlet-4.1.6.RELEASE
    spring-websocket-4.1.6.RELEASE
    OutPut
    Your Message : I am object A
    Your Message : I am object A

    The Prototype scope:

       You need to change only Beans.xml and set scope="prototype" then output will be: OutPut
    Your Message : I am object A
    Your Message : null

    « Previous | 1 | 2 | 3 | Next »


    copyright © 2014 - all rights riserved by javatechnologycenter.com