Let us discussed, one by one from our end practically.
Singleton bean scope
Step1. Create SpringFile.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 definitions here -->
<bean id="JobSeekerId" class="com.ps.model.JobSeeker" scope="singleton"></bean>
</beans>
Step2. Create POJO class
package com.ps.model;
public class JobSeeker {
private int jsId;
private String jsName;
public int getJsId() {
return jsId;
}
public void setJsId(int jsId) {
this.jsId = jsId;
}
public String getJsName() {
return jsName;
}
public void setJsName(String jsName) {
this.jsName = jsName;
}
public JobSeeker() {
System.out.println("********Object is creating******");
}
public void diplayJSInfo()
{
System.out.println("Job Seeker ID: "+this.getJsId()+" Job Seeker Name: "+this.getJsName());
}
}
Step3. Create main class
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ps.model.JobSeeker;
public class SpringApp {
public static void main(String[] args) {
System.out.println("*****Create Application context Object******");
//ApplicationContext object pre initialize and load all beans from springFile.xml
ApplicationContext appContext = new ClassPathXmlApplicationContext("SpringFile.xml");
System.out.println("*****Created Application context Object******");
JobSeeker jobSeekerObj=(JobSeeker)appContext.getBean("JobSeekerId");
jobSeekerObj.setJsId(100);
jobSeekerObj.setJsName("Heera");
jobSeekerObj.diplayJSInfo();
JobSeeker jobSeekerObj1=(JobSeeker)appContext.getBean("JobSeekerId");
jobSeekerObj1.setJsId(101);
jobSeekerObj1.setJsName("Babu");
jobSeekerObj1.diplayJSInfo();
}
}
Sample OUTPUT:
*****Create Application context Object******
********Object is creating******
*****Created Application context Object******
Job Seeker ID: 100 Job Seeker Name: Heera
Job Seeker ID: 101 Job Seeker Name: Babu
Note:-
From the above output, we can see that only one time constructor has been called, while application has initialized, So that we can say that all beans are initialized or loaded by spring bean container as the application started (or can say on application start time).Prototype bean scope
Step4. From the above code, we are changing only springfile.xml as 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.xsd">
<!-- bean definitions here -->
<bean id="JobSeekerId" class="com.ps.model.JobSeeker" scope="prototype"></bean>
</beans>
Sample OUTPUT:
*****Create Application context Object******
*****Created Application context Object******
********Object is Creating******
Job Seeker ID: 100 Job Seeker Name: Heera
********Object is Creating******
Job Seeker ID: 101 Job Seeker Name: Babu
Note:-
From the above, output we can say that in prototype bean scope, the application context object is created, then one by one each bean object are created by each request comes in the application.