Reading:  

Quick to the point introduction to Spring Framework


Working with Bean Scopes

In spring to define a bean user has the option of declaring a scope for the bean. The types of bean scopes are:

  • Singleton: this scope returns a single bean instance per Spring IoC container.
  • Prototype: this scope returns a new bean instance for a single bean definition.
  • Request: this scope returns a single bean instance per HTTP request.
  • Session: this scope returns a single bean instance per HTTP session.
  • globalSession: this scope returns a single bean instance per global HTTP session.

User may only work with the Spring’s core scope in most of the situation: singleton and prototype where singleton is a default.

The singleton scope:

The IoC container of Spring creates single instance of the object which is defined by the definition of bean. This single instance is stored in a cache of singleton beans:

Syntax:

<!-- A bean definition with singleton scope -->
<bean id="..." class="..." scope="singleton">
    <!-- collaborators and configuration for this bean go here -->
</bean>
 

Let's write some code to learn about singleton scope, here is my java class class Customer.

package pack1;  
  
public class Customer {  
private String cust_name;  
  
public String getCname() {  
    System.out.println("Customer Name: "+ cust_name);  
  }  
  
public void setCname(String cust_name) {  
    this.cust_name = cust_name;  
   }  
}  
 

In my test class which for some reason I have named it ExmTest, I will make use of above class and call its methods as shown below

package pack1;  
  
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
  
public class ExmTest {  
public static void main(String[] args) {  
ApplicationContext cont = 
             new ClassPathXmlApplicationContext("SingleBeans.xml");
    // Get bean object from ApplicationContext
    Customer firstCust=(Customer)cont.getBean("Customer");  
    firstCust.setCname("Nimrit");  
    firstCust. getCname();
// Get bean object from ApplicationContext Customer secondCust=(Customer)cont.getBean("Customer"); firstCust.setCname("Yuvi"); firstCust. getCname(); } }
 

In our configuration file, we will declare our Bean class, i.e. Customer with scope singleton as shown below

<?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="Customer" class="pack1.Customer" 
      scope="singleton">
</bean>  
  </beans>  
 

Now when we execute our test class, this is what we get

Result

Customer name: Nimrit

Customer name: Yuvi

 

The prototype scope:

The IoC container of Spring creates new bean instance of the object any time if requested. User need to set the property of scope to prototype in the configuration file of bean, as shown below:

Syntax: 

<?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="..." class="..." 
      scope="prototype">
</bean>  
  </beans>  


Let's check our Customer class again

package pack1;  
  
public class Customer {  
private String cust_name;  
  
public String getCname() {  
    System.out.println("Customer Name: "+ cust_name);  
  }  
  
public void setCname(String cust_name) {  
    this.cust_name = cust_name;  
   }  
}  

And our ExmTest class, source for which is shown below

package pack1;  
  
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
  
public class ExmTest {  
public static void main(String[] args) {  
ApplicationContext cont = 
             new ClassPathXmlApplicationContext("SingleBeans.xml");

    Customer firstCust=(Customer)cont.getBean("Customer");  
    firstCust.setCname("Nimrit");  
    firstCust. getCname();
    Customer secondCust=(Customer)cont.getBean("Customer");  
    firstCust.setCname("Yuvi");  
    firstCust. getCname();
     }  
}  

And finally our configuration file "SingleBeans.xml", a refence of which is used in our ExtTest class above

<?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="Customer" class="pack1.Customer" 
      scope=" prototype ">
</bean>  
  </beans>  

Compile and run the program will get the below result:

Result:

Customer Name: Nimrit

Customer Name: Yuvi

 

In the next part of this tutorial, we will check out and understand Bean Life Cycle within Spring framework.

Description

This tutorial covers various topics releated to Spring Framework as listed below

  • Framework Overview
  • Modules
  • Environment Setup
  • Saying Hello World with Spring
  • IoC Containers
  • Bean Definition
  • Bean Scopes 
  • Bean Life Cycle
  • Bean Post Processors
  • Bean Definition Inheritance
  • Dependency Injection
  • Injecting Inner Beans
  • Injecting Collection
  • Beans Auto-Wiring
  • Annotation Based Configuration
  • Java Based Configuration
  • Event Handling in Spring
  • Custom Events in Spring
  • JDBC Framework Overview
  • Transaction Management

 

 



Prerequisites

Prior knowledge of Java is essential

Audience

Beginners or students seeking quick introduction to Spring

Learning Objectives

This tutorial is for beginners seeking quick introduction to Spring Framework.

Author: Subject Coach
Added on: 22nd Jun 2015

You must be logged in as Student to ask a Question.

None just yet!