In prototype scope whenever a bean is referenced (either in <property> tags or in getBean() method), a new instance of that bean is returned instead of returning the same instance again and again.
Consider following figure.
In above figure there are four beans namely bean1, bean2, bean3 and bean4. Bean1 is defined with prototype scope and is being injected into bean2, bean3, and bean4. Then, unlike singleton beans, each of bean 2,3 and 4 will have their own instance of bean1. Thus there would be three separate instances of bean1 instead of one common instance.
We can also check this by code as shown in following examples.
Example1: (When a bean is referenced in <property> tags)
bean1.java
package examples.beanscopes;
public class Bean1 {
public void show(){
System.out.println("This is Prototype scoped bean");
}
}
public class Bean1 {
public void show(){
System.out.println("This is Prototype scoped bean");
}
}
bean2.java
package examples.beanscopes;
public class Bean2 {
Bean1 memberBean;
public void setMemberBean(Bean1 memberBean){
this.memberBean = memberBean;
}
public Bean1 getMemberBean(){
return memberBean;
}
}
public class Bean2 {
Bean1 memberBean;
public void setMemberBean(Bean1 memberBean){
this.memberBean = memberBean;
}
public Bean1 getMemberBean(){
return memberBean;
}
}
bean3.java
package examples.beanscopes;
public class Bean3 {
Bean1 memberBean;
public void setMemberBean(Bean1 memberBean){
this.memberBean = memberBean;
}
public Bean1 getMemberBean(){
return memberBean;
}
}
public class Bean3 {
Bean1 memberBean;
public void setMemberBean(Bean1 memberBean){
this.memberBean = memberBean;
}
public Bean1 getMemberBean(){
return memberBean;
}
}
bean4.java
package examples.beanscopes;
public class Bean4 {
Bean1 memberBean;
public void setMemberBean(Bean1 memberBean){
this.memberBean = memberBean;
}
public Bean1 getMemberBean(){
return memberBean;
}
}
public class Bean4 {
Bean1 memberBean;
public void setMemberBean(Bean1 memberBean){
this.memberBean = memberBean;
}
public Bean1 getMemberBean(){
return memberBean;
}
}
ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="prototypeBean" class="examples.beanscopes.Bean1" scope="prototype"/>
<bean id="bean2" class="examples.beanscopes.Bean2">
<property name="memberBean" ref="prototypeBean"/>
</bean>
<bean id="bean3" class="examples.beanscopes.Bean3">
<property name="memberBean" ref="prototypeBean"/>
</bean>
<bean id="bean4" class="examples.beanscopes.Bean4">
<property name="memberBean" ref="prototypeBean"/>
</bean>
</beans>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="prototypeBean" class="examples.beanscopes.Bean1" scope="prototype"/>
<bean id="bean2" class="examples.beanscopes.Bean2">
<property name="memberBean" ref="prototypeBean"/>
</bean>
<bean id="bean3" class="examples.beanscopes.Bean3">
<property name="memberBean" ref="prototypeBean"/>
</bean>
<bean id="bean4" class="examples.beanscopes.Bean4">
<property name="memberBean" ref="prototypeBean"/>
</bean>
</beans>
MainApp.java
package examples.beanscopes;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String args[]){
ApplicationContext ctx=new ClassPathXmlApplicationContext("resources/ApplicationContext.xml");
Bean2 bean2Obj = (Bean2)ctx.getBean("bean2");
Bean3 bean3Obj = (Bean3)ctx.getBean("bean3");
Bean4 bean4Obj = (Bean4)ctx.getBean("bean4");
if(bean2Obj.memberBean == bean3Obj.memberBean)
System.out.println("Bean2 and Bean3 contain same instance of prototype scoped bean1");
else
System.out.println("Bean2 and Bean3 DOES NOT CONTAIN same instance of prototype scoped bean1");
if(bean3Obj.memberBean == bean4Obj.memberBean)
System.out.println("Bean3 and Bean4 contain same instance of prototype scoped bean1");
else
System.out.println("Bean3 and Bean4 DOES NOT CONTAIN same instance of prototype scoped bean1");
if(bean2Obj.memberBean == bean4Obj.memberBean)
System.out.println("Bean2 and Bean4 contain same instance of prototype scoped bean1");
else
System.out.println("Bean2 and Bean4 DOES NOT CONTAIN same instance of prototype scoped bean1");
}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String args[]){
ApplicationContext ctx=new ClassPathXmlApplicationContext("resources/ApplicationContext.xml");
Bean2 bean2Obj = (Bean2)ctx.getBean("bean2");
Bean3 bean3Obj = (Bean3)ctx.getBean("bean3");
Bean4 bean4Obj = (Bean4)ctx.getBean("bean4");
if(bean2Obj.memberBean == bean3Obj.memberBean)
System.out.println("Bean2 and Bean3 contain same instance of prototype scoped bean1");
else
System.out.println("Bean2 and Bean3 DOES NOT CONTAIN same instance of prototype scoped bean1");
if(bean3Obj.memberBean == bean4Obj.memberBean)
System.out.println("Bean3 and Bean4 contain same instance of prototype scoped bean1");
else
System.out.println("Bean3 and Bean4 DOES NOT CONTAIN same instance of prototype scoped bean1");
if(bean2Obj.memberBean == bean4Obj.memberBean)
System.out.println("Bean2 and Bean4 contain same instance of prototype scoped bean1");
else
System.out.println("Bean2 and Bean4 DOES NOT CONTAIN same instance of prototype scoped bean1");
}
}
Output:
Bean2 and Bean3 DOES NOT CONTAIN same instance of prototype scoped bean1
Bean3 and Bean4 DOES NOT CONTAIN same instance of prototype scoped bean1
Bean2 and Bean4 DOES NOT CONTAIN same instance of prototype scoped bean1
Bean3 and Bean4 DOES NOT CONTAIN same instance of prototype scoped bean1
Bean2 and Bean4 DOES NOT CONTAIN same instance of prototype scoped bean1
This shows that each of bean2, 3 and 4 have their own instances of bean1. Had we defined bean1 as singleton then output would be:
Bean2 and Bean3 contain same instance of prototype scoped bean1
Bean3 and Bean4 contain same instance of prototype scoped bean1
Bean2 and Bean4 contain same instance of prototype scoped bean1
Bean3 and Bean4 contain same instance of prototype scoped bean1
Bean2 and Bean4 contain same instance of prototype scoped bean1
Example2: (When bean is referenced in getBean() method)
bean1.java
package examples.beanscopes;
public class Bean1 {
public void show(){
System.out.println("This is Prototype scoped bean");
}
}
public class Bean1 {
public void show(){
System.out.println("This is Prototype scoped bean");
}
}
ApplicationContext.java
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="prototypeBean" class="examples.beanscopes.Bean1" scope="prototype"/>
</beans>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="prototypeBean" class="examples.beanscopes.Bean1" scope="prototype"/>
</beans>
MainApp.java
package examples.beanscopes;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String args[]){
ApplicationContext ctx=new ClassPathXmlApplicationContext("resources/ApplicationContext.xml");
Bean1 obj1 = (Bean1)ctx.getBean("prototypeBean");
Bean1 obj2 = (Bean1)ctx.getBean("prototypeBean");
if(obj1 == obj2)
System.out.println("Both are same instances");
else
System.out.println("Both are different instances");
}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String args[]){
ApplicationContext ctx=new ClassPathXmlApplicationContext("resources/ApplicationContext.xml");
Bean1 obj1 = (Bean1)ctx.getBean("prototypeBean");
Bean1 obj2 = (Bean1)ctx.getBean("prototypeBean");
if(obj1 == obj2)
System.out.println("Both are same instances");
else
System.out.println("Both are different instances");
}
}
Output:
Both are different instances
Here also if we had defined bean1 as singleton then output would be:
Both are same instances
Thus whenever a prototyped bean is referenced,a new instance of this prototyped bean is created and returned, instead of returning same instance again and again.
I would like to know your comments and if you liked the article then please share it on social networking buttons.
No comments:
Post a Comment