Friday 11, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Saturday, 8 March 2014

Spring singleton Bean Scope

In this tutorial we are going to see Spring singleton Bean scope with example program.
Only one shared instance of a singleton bean is managed, and all requests for beans with an id or ids matching that bean definition result in that one specific bean instance being returned by the Spring container.
To put it another way, when you define a bean definition and it is scoped as a singleton, the Spring IoC container creates exactly one instance of the object defined by that bean definition. This single instance is stored in a cache of such singleton beans, and all subsequent requests and references for that named bean return the cached object.
Spring singleton Bean
Spring's concept of a singleton bean differs from the Singleton pattern as defined in the Gang of Four (GoF) patterns book. The GoF Singleton hard-codes the scope of an object such that one and only one instance of a particular class is created per ClassLoader. The scope of the Spring singleton is best described as per container and per bean. This means that if you define one bean for a particular class in a single Spring container, then the Spring container creates one and only one instance of the class defined by that bean definition. The singleton scope is the default scope in Spring.
Example Bean.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.xsd">

<bean id="singletonBean" class="com.javatutorialscorner.spring.SingletonBean" >
</bean>
<!-- the following is equivalent, though redundant (singleton scope is the default) -->
<bean id="singletonBean" class="com.javatutorialscorner.spring.SingletonBean" scope="singleton">
</bean>

</beans>

Step by step procedure to create Spring Program using Eclipse given below.

Follow the Simple steps.

1. Select File –> New –> Java Project from your Eclipse IDE.

2.Create project called SingletonBeanScope.

3. Add the following jars into your build path.

commons-logging-1.1.3.jar

spring-beans-3.2.6.RELEASE.jar

spring-context-3.2.6.RELEASE.jar

spring-context-support-3.2.6.RELEASE.jar

spring-core-3.2.6.RELEASE.jar

spring-expression-3.2.6.RELEASE.jar

4. Now create package com.javatutorialscorner.spring under SingletonBeanScope Project

5. Next create SingletonBean class under com.javatutorialscorner.spring package

SingletonBean.java

package com.javatutorialscorner.spring;

public class SingletonBean {
private String sayHello;

public void getSayHello() {
System.out.println("Java Tutorials Corner " + sayHello);
}

public void setSayHello(String sayHello) {
this.sayHello = sayHello;
}
}

6. Now Create RunApp class which contains Bean configuration path to load configuration from Bean.xml file

RunApp.java

package com.javatutorialscorner.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class RunApp {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
SingletonBean singletonBean = (SingletonBean)context.getBean("singletonBean");
singletonBean.setSayHello("Spring Tutorials");
singletonBean.getSayHello();

SingletonBean singletonBean1 = (SingletonBean)context.getBean("singletonBean");
singletonBean1.getSayHello();

}

}

In above program

First step I created Application context using ClassPathXmlApplicationContext  which load configuration from bean file  which located in class path of apllication, It take care of creating and initializing all the objects (Beans) declared in bean.xml

Next getBean() method used to get particular bean from created context. This method used bean name as parameter and returns generic object. We can caste to actual object.

Once Object Created you can access any method from that class.

7. Create Bean.xml at where your class files created (ClassPathXmlApplicationContext tries to load bean file from class path).

Bean.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.xsd">

<bean id="singletonBean" class="com.javatutorialscorner.spring.SingletonBean" scope="singleton">
</bean>
</beans>

Bean.xml is default name given to bean configuration file. You can choose any name for your bean.xml but you can use file name in main application to create context and your file available in your class path must be same.

Now you can run the program see the following output in console.

Java Tutorials Corner Spring Tutorials
Java Tutorials Corner Spring Tutorials

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • Spring Default initialization and destroy callback methodsIn this tutorial we are going to see about Spring Default (Life Cycle) initialization and destroy method with example program.When you write initialization and destroy method callbacks that do not use the Spring-specific Init… Read More
  • Spring DI - Injecting Inner BeansIn this tutorial we are going to see about Spring Injecting Inner Beans .Inner BeanA <bean/> element inside the <property/> or <constructor-arg/> elements defines a so-called inner bean. An inner bean defi… Read More
  • Spring singleton Bean ScopeIn this tutorial we are going to see Spring singleton Bean scope with example program.Only one shared instance of a singleton bean is managed, and all requests for beans with an id or ids matching that bean definition result … Read More
  • Spring Bean Scope In this tutorial we are going to see the overview of Spring bean scope. When you create bean definition in Spring, you can declare the scope of bean. You can control not only the various dependencies and configuration value… Read More
  • Spring DI - Setter-based Dependency InjectionIn this tutorial we are going to see about Spring setter based dependency injection with example program,Setter-based DI is accomplished by the container calling setter methods on your beans after invoking a no-argument const… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Spring singleton Bean Scope Rating: 5 Reviewed By: eHowToNow