Home > Spring > spring scope singleton example

spring scope singleton example

spring scope singleton example

In the spring bean scope, the scope of the bean is used to decide which type of instance of the bean should be returned from the Spring container to the caller.

If a scope is configured as the singleton, the Spring IoC container creates exactly one instance of the object defined by this bean definition. This single instance is stored in a cache of these singleton beans, and subsequent requests and references for that named bean return the cached object.

The default range is always the singleton. However, when you need a single instance of a bean, you can configure the scope to be in the configuration file, as shown in the following code snippet:

<!-- To create single object --> 
<bean id="t" class="beans.Test" scope="singleton"/>

Types of bean scope supported:

singleton – Returns a single bean instance per IoC Spring container
prototype – Returns a new bean instance whenever requested
request – Returns a single bean instance by HTTP request. *
session – Returns a single bean instance per HTTP session. *
globalsession – Returns a single bean instance per global HTTP session. *
* means only valid in the context of a Spring SpringContext application

In most cases, you can only handle the main scope of Spring – singleton, prototype, and the default scope is singleton.

singleton bean scope example:

We are using Ecplise IDE and some required jars to develop below application.

spring scope prototype example

Ecplise IDE: Ecplise Luna

Jars: Placed below jar in “Referenced Libraries” Folder

  1. commons-logging-1.2
  2. spring-beans-5.0.8.RELEASE
  3. spring-context-5.0.8.RELEASE
  4. spring-core-5.0.8.RELEASE

To create this example, we have created 3 files inside the src directory.

  1. spring.xml
  2. Test .java
  3. client.java


	<!-- To create single object -->
	 <bean id="t" class="beans.Test" scope="singleton"/>

Test .java

package beans;
public class Test 
       System.out.println("Test Constructor");
   public void hello()
     System.out.println("Hello World");


package beans;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class Client 
	public static void main(String[] args) 
		//Find xml file
		Resource resource=new ClassPathResource("resources/spring.xml");
		//Load xml into container
		BeanFactory factory =new XmlBeanFactory(resource);
		//Create Object of Test Class with reference of "t"
		Object o=factory.getBean("t");
		Object o1=factory.getBean("t");
		Object o2=factory.getBean("t");
		//Compare object of same class
		System.out.println("Compare object of same class");
		Test t =(Test)o;


Test Constructor
Compare object of same class
Hello World

Related Topics:-

You may also like :-
We are group of application developer and admin.We are providing good content of information about IT technology and services .

Leave a Reply

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