Spring Bean:
In Spring Framework, the bean is a backbone. Spring bean is an object of a class, whose life cycle is maintained by spring IOC container. The bean object is instantiated, assembled and destroyed by bean container. These beans objects are created by configuration metadata that we give to a container.
In most cases, we only deal with the Spring’s core scope – singleton and prototype, and the default scope is singleton.
What is the spring bean scope?
The default bean scope in spring is Singleton.1. Singleton bean Scope
- Singleton bean Scope is a bean scope, in which a single bean will create an object instance per Spring IOC Container.
- Singleton bean Scope returns a single bean instance per Spring IoC container and so that,
- 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.
2. ProtoType bean scope
- prototype bean scope is another bean scope, in which a single bean will create any number of object instances.
- It returns a new bean instance each time when requested.
- It does not store any cache version like a singleton.
3. Request bean scope
- Request bean scope is a bean, in which a single bean handles the lifecycle of a single HTTP request.
- It means each and every HTTP request will have its own instance of a bean created of the back of a single bean definition.
- Only valid in the context of a web-aware Spring ApplicationContext. It returns a single bean instance per HTTP request.
4. Session bean scope
- Session bean scope is a scope bean, in which a single bean definition to the lifecycle of an HTTP session.
- Only valid in the context of a web-ware Spring ApplicationContext.
- It returns a single bean instance per HTTP session (User level session).
5. Global session
- Global session is a bean scope, in which a single bean definition to the lifecycle of a global HTTP session. It's typically valid only in when used in a portlet context.
- Only valid in the context of a web-aware Spring ApplicationContext.
- It returns a single bean instance per global HTTP session.
- It is only valid in the context of a web-aware Spring ApplicationContext (Application level session).
Example of default bean scope
Create a model class, that will bean in configuration file.
package com.model;
public class JobSeeker {
private int js_id;
private String name;
public JobSeeker() {
System.out.println("New Object creating by Application*");
}
public int getJs_id() {
return js_id;
}
public void setJs_id(int js_id) {
this.js_id = js_id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void diplay()
{
System.out.println("*****Displaying info******");
System.out.println("Name of Job Seeker: "+this.getName()+" and Id: "+this.getJs_id());
}
}
Create Spring Beans xml file
Use the link to create beans xml.
click to go spring.io
<?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="jobseek" class="com.model.JobSeeker">
<property name="js_id" value="100"></property>
<property name="name" value="Heera"></property>
</bean>
</beans>
Create main class that will run the program.
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestByMainClient {
public static void main(String[] args) {
ApplicationContext aContext =new ClassPathXmlApplicationContext("springBeansFile.xml");
JobSeeker obj=(JobSeeker)aContext.getBean("jobseek");
obj.setJs_id(101);
obj.setName("John");
obj.diplay();
JobSeeker obj2=(JobSeeker)aContext.getBean("jobseek");
obj2.setJs_id(102);
obj2.setName("Jonny");
obj.diplay();
}
}
Sample output
Dec 23, 2018 12:56:10 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@e2144e4: startup date [Sun Dec 23 12:56:10 IST 2018]; root of context hierarchy
Dec 23, 2018 12:56:10 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [springBeansFile.xml]
New Object creating by Application*
*****Displaying info******
Name of Job Seeker: John and Id: 101
*****Displaying info******
Name of Job Seeker: Jonny and Id: 102
NOTE:
Here, above program contains two object information, but spring container has created only one object instance. If we create two objects same like above by new keyword then same object will return same values.
public class TestInCoreJava {
public static void main(String[] args) {
JobSeeker obj=new JobSeeker();
obj.setJs_id(101);
obj.setName("John");
obj.diplay();
JobSeeker obj2=new JobSeeker();
obj2.setJs_id(102);
obj2.setName("Jonny");
obj.diplay();
}
}
Output:
New Object creating by Application*
*****Displaying info******
Name of Job Seeker: John and Id: 101
New Object creating by Application*
*****Displaying info******
Name of Job Seeker: John and Id: 101
Finally, let’s go to bean property setter value in xml, what about these properties if we have given in spring, then remove some setter from the code.
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestByMainClient {
public static void main(String[] args) {
ApplicationContext aContext =new ClassPathXmlApplicationContext("springBeansFile.xml");
JobSeeker obj=(JobSeeker)aContext.getBean("jobseek");
obj.diplay();
JobSeeker obj2=(JobSeeker)aContext.getBean("jobseek");
obj2.setJs_id(102);
obj2.setName("Jonny");
obj.diplay();
}
}
Expected-output:
Dec 23, 2018 2:44:20 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@e2144e4: startup date [Sun Dec 23 14:44:20 IST 2018]; root of context hierarchy
Dec 23, 2018 2:44:21 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [springBeansFile.xml]
New Object creating by Application*
*****Displaying info******
Name of Job Seeker: Heera and Id: 100
*****Displaying info******
Name of Job Seeker: Jonny and Id: 102