Spring supports the following 5 types of bean scopes and the default scope is singleton

  1. singleton – Return a single bean instance per Spring IoC container
  2. prototype – Return a new bean instance each time when requested
  3. request – Return a single bean instance per HTTP request.
  4. session – Return a single bean instance per HTTP session.
  5. globalSession – Return a single bean instance per global HTTP session.

Singleton scope example:
Example:

  1. Create the Engine class
  2. package com.java2learn.core;
    
    public class Engine {
    	
    	private String engineID;
    	private String validity;
    	
    	public String getEngineID() {
    		return engineID;
    	}
    	public void setEngineID(String engineID) {
    		this.engineID = engineID;
    	}
    	public String getValidity() {
    		return validity;
    	}
    	public void setValidity(String validity) {
    		this.validity = validity;
    	}
    	@Override
    	public String toString() {
    		return "Engine [engineID=" + engineID + ", validity=" + validity + "]";
    	}
    }
    
    
  3. Configure Engine class in Spring-Core.xml configuration file.
  4. <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="engine" class="com.java2learn.core.Engine" scope="singleton">
    		<property name="engineID" value="123"></property>
    		<property name="validity" value="3Yrs."></property>
    	</bean>
    </beans>
    
  5. Create the client and run it.
  6. package com.java2learn.app;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import com.java2learn.core.Engine;
    public class ApplicationContextDemo {
    
    	public static void main(String[] args) {
    
    		ApplicationContext applicationContext=new ClassPathXmlApplicationContext("Spring-Core.xml");
    		
    		Engine engine=(Engine)applicationContext.getBean("engine");
    		System.out.println("Validity::"+engine.getValidity());
    		//setting value to the validaity.
    		engine.setValidity("10 Yrs.");
    		System.out.println("Validity::"+engine.getValidity());
    		System.out.println("Engine ID::"+engine.getEngineID());
    		
    		//retrieve bean again 
    		Engine engine1=(Engine)applicationContext.getBean("engine");
    		System.out.println("Validity::"+engine1.getValidity());
    	}
    }
    

    Output:
    Validity::3Yrs.
    Validity::10 Yrs.
    Engine ID::123
    Validity::10 Yrs.

Prototype scope example:
Example:

  1. Create the Engine class
  2. package com.java2learn.core;
    
    public class Engine {
    	
    	private String engineID;
    	private String validity;
    	
    	public String getEngineID() {
    		return engineID;
    	}
    	public void setEngineID(String engineID) {
    		this.engineID = engineID;
    	}
    	public String getValidity() {
    		return validity;
    	}
    	public void setValidity(String validity) {
    		this.validity = validity;
    	}
    	@Override
    	public String toString() {
    		return "Engine [engineID=" + engineID + ", validity=" + validity + "]";
    	}
    }
    
    
  3. Configure Engine class in Spring-Core.xml configuration file.
  4. <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="engine" class="com.java2learn.core.Engine" scope="prototype"/>
    </beans>
    
  5. Create the client and run it.
  6. package com.java2learn.app;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import com.java2learn.core.Engine;
    public class ApplicationContextDemo {
    
    	public static void main(String[] args) {
    
    		ApplicationContext applicationContext=new ClassPathXmlApplicationContext("Spring-Core.xml");
    		
    		Engine engine=(Engine)applicationContext.getBean("engine");
    		//setting value to the validaity.
    		engine.setValidity("10 Yrs.");
    		System.out.println("Validity::"+engine.getValidity());
    		
    		//retrieve bean again 
    		Engine engine1=(Engine)applicationContext.getBean("engine");
    		System.out.println("Validity::"+engine1.getValidity());
    	}
    }
    

    Output:
    Validity::10 Yrs.
    Validity::null