Spring

Spring - Bean Scopes

Spring - Bean Scopes

When characterizing a <bean>, you have the choice of proclaiming a degree for that bean. For instance, to drive Spring to create another bean occasion each time one is required, you should announce the bean's extension quality to be the prototype. Likewise, assuming you need Spring to return a similar bean occasion each time one is required, you should announce the bean's extension characteristic as a singleton. The Spring Framework upholds the accompanying five extensions, three of which are accessible provided that you utilize a web-mindful ApplicationContext. 

Scope and its purpose

  • singleton 

These scopes the bean definition to a solitary example for every Spring IoC holder (default). 

  • prototype

This scopes a solitary bean definition to have quite a few article examples. 

  • request

This scopes a bean definition to an HTTP demand—just substantial for a web-mindful Spring ApplicationContext. 

  • session

This scopes a bean definition to an HTTP meeting—just substantial regarding a web-mindful Spring ApplicationContext

  • global-session 

This scopes a bean definition to a worldwide HTTP meeting—just substantial regarding a web-mindful Spring ApplicationContext. 

The singleton scope 

On the off chance that an extension is set to singleton, the Spring IoC compartment makes precisely one occasion of the item characterized by that bean definition. This single occurrence is put away in a reserve of singleton beans, and every ensuing solicitation and reference for that named bean return the stored object. The default degree is consistently singleton. Nonetheless, when you need only one bean occurrence, you can set the extension property to a singleton in the bean arrangement document. 

With the below steps, you can create a Spring application:

#1: Create a project named SpringDemo and make a bundle demo under the src envelope in the made task. 

#2: Add required Spring libraries utilizing Add External JARs choice as clarified in the Spring Hello World Demo Example part. 

#3: Create Java classes HelloWorld and MainApp under the demo bundle. 

#4: Create Beans setup record Beans.xml under the src envelope. 

#5: The last advance is to make the substance of all the Java records and Bean Configuration document and run the application 

Example

public class HelloWorld {
   private String message;

   public void setMessage(String message){
      this.message  = message;
   }
   public void getMessage(){
      System.out.println( message);
   }
}

MainApp Code:

package demo;

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 objA = (HelloWorld) context.getBean("helloWorld");

      objA.setMessage("Object A");
      objA.getMessage();

      HelloWorld objB = (HelloWorld) context.getBean("helloWorld");
      objB.getMessage();
   }
}
xml Code:
<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 = "demo.HelloWorld" scope = "singleton">
   </bean>

</beans>

The prototype scope 

On the off chance that the degree is set to model, the Spring IoC holder makes another bean case of the article each time a solicitation for that particular bean is made. When in doubt, utilize the model degree for all state-full beans and the singleton scope for stateless beans. To characterize a prototype extension, you can set the degree property to model in the bean setup document 

#1: Create a project with the name SpringDemo and make a bundle demo under the src envelope in the made task. 

#2: Add required Spring libraries utilizing Add External JARs alternative as clarified in the Spring Hello World Example part. 

#3: Create Java classes HelloWorld and MainApp under the demo bundle. 

#4: Create Beans arrangement record Beans.xml under the src organizer. 

#5: The last advance is to make the substance of all the Java records and Bean Configuration document and run the application

The HelloWorld.Java and MainApp.Java code are the same; however, the XML code is as follows:

<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 = "demo.HelloWorld" scope = "prototype">
   </bean>

</beans>