Spring bean Scope

Scopes are there from the birth of programming language,we call it design patterns. According to gang of4 book there are 3 types of design patterns: Creational, structural, Behavioural design patterns.In creational design pattern there are singleton and prototype pattern and etc are there which we use in certain situations. In spring to use design patterns we don’t need to write code for that we just call it.By default spring is providing those for us. If we create any bean by default its scope is a singleton, If you want to modify the default one you can specify the scope type with indiaval beans in beans.xml. lets see an example for different type of beans scopes,

lets continue with the previous code.

beans.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">
<p style="text-align: justify;">
<bean id="person" class="com.sourceforopensource.pojos.Person"
scope="singleton"> //here scope is singleton which means only once instance created when we call that bean
//the same instance we will get every time, we can change it to "prototype" prototype that means we will get 
//new instance of bean every time we call the prototype bean.
<constructor-arg value="777" name="id" type="int">
</constructor-arg>
<constructor-arg value="Mary" name="name" type="String"></constructor-arg>
<property value="321" name="taxId">
</property>
<property name="address" ref="address"></property>
</bean></p>
<bean id="address" class="com.sourceforopensource.pojos.Address">
<constructor-arg name="street" value="1 Church Lane"></constructor-arg>
<constructor-arg name="postcode" value="9876"></constructor-arg>
</bean>

</beans>

Address.java


package com.sourceforopensource.pojos;

public class Address {
private String street;
private String postcode;

public Address(String street, String postcode) {
this.street = street;
this.postcode = postcode;
}

@Override
public String toString() {
return "Address [street=" + street + ", postcode=" + postcode + "]";
}
}

Person.java


package com.sourceforopensource.pojos;

public class Person {

private int id;
private String name;
private int taxId;
private Address address;

public Person() {
}
public Person(int id, String name) {
this.id = id;
this.name = name;
}

public void speak() {
System.out.println("Hello! I'm a person.");
}

public void setTaxId(int taxId) {
this.taxId = taxId;
}

public void setAddress(Address address) {
this.address = address;
}

@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", taxId=" + taxId
+ ", address=" + address + "]";
}
}

App.java


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class App {

public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext("com/sourceforopensource/pojos/beans.xml");
Person person1 = (Person)context.getBean("person");
Person person2 = (Person)context.getBean("person");

person1.setTaxId(666); //we are setting taxid here to person1, when we call person2 the
                       //taxid is assigned to person2 also, this happen only with singleton bean.

System.out.println(person2);
//In singleton scope "bean" is instantiated only once, and every time we 
((ClassPathXmlApplicationContext)context).close();
}

}

output: for singleton,

Person [id=777, name=Mary, taxId=666, address=Address [street=1 Church Lane, postcode=9876]]

output: for prototype,

Person [id=777, name=Mary, taxId=321, address=Address [street=1 Church Lane, postcode=9876]]

Balakrishna Pendyala

Author: Balakrishna Pendyala

Balakrishna is a Software Engineer working in Media Streaming Domain, and also worked on Banking and Health Care domains. His areas of interest include Object Oriented Design, SOLID Design principles, Restful Web Services and Open Source softwares including Spring, Spring Boot, and Hibernate.

If you found an error, highlight it and press Shift + Enter or click here to inform us.

Leave a Reply

Your email address will not be published. Required fields are marked *