While defining Spring Bean we have options to define scopes for each bean. Spring supports 5 bean scopes.
- singleton – This scope returns a single bean instance per Spring IoC container(Default Scope)
- prototype – This scope returns a new bean instance every time
- request – This scope returns a single bean instance for each HTTP request.
- session – This scope returns a single bean instance for each HTTP session.
- globalSession – This scope returns a single bean instance for each global HTTP session.
Last three scopes (request, session, globalSession) can be used only with web-aware ApplicationContext.
We will learn a bit more about singleton and prototype scopes in this article
Folder Structure:
- Create a new Java Project “SpringCoreTutorial” and create a package for our src files “com.javainterviewpoint“
- Add the required libraries to the build path. Java Build Path ->Libraries ->Add External JARs and add the below jars.
commons-logging-1.1.1.jar
spring-beans-3.2.9.RELEASE.jar
spring-core-3.2.9.RELEASE.jar
spring-context-3.2.9.RELEASE.jar
spring-expression-3.2.9.RELEASE.jar - Create the Java classes Book.java and ClientLogic.java under com.javainterviewpoint folder.
- Place our configuration file SpringConfig.xml in the src directory
Book.java
Book class will have all the book details such title, author, publications and its corresponding POJO’s. The getBookDetails() method will display the book information which is set.
package com.javainterviewpoint; public class Book { private String title; private String author; private String publications; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getPublications() { return publications; } public void setPublications(String publications) { this.publications = publications; } public void getBookDetails(String book) { System.out.println("**Published "+book+" Details**"); System.out.println("Book Title : "+title); System.out.println("Book Author : "+author); System.out.println("Book Publications : "+publications); } }
ClientLogic.java
package com.javainterviewpoint; import java.util.Iterator; import java.util.Set; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; public class ClientLogic { public static void main(String args[]) { //Read the configuration file Resource resource = new ClassPathResource("SpringConfig.xml"); //Read all the bean definition BeanFactory bf = new XmlBeanFactory(resource); //Get the book1 Instance Book book1 = (Book)bf.getBean("book"); book1.setTitle("Core Spring"); book1.setAuthor("Java Interview Point"); book1.setPublications("JIP"); //Printing book1 details book1.getBookDetails("book1"); //Getting book2 instance Book book2 = (Book)bf.getBean("book"); //Printing book2 details book2.getBookDetails("book2"); } }
- Resource class reads our Configuration File(SpringConfig.xml)
- BeanFactory class read all the bean definition mentioned in the config file.
- Get the Book Class instance by calling the getBean() method over the bean factory(book1 and book2).
- The String passed to getBean() method should be equivalent to the id defined in the SpringConfig.xml
- We will be setting values to attributes using book1 instance and call getBookDetails() method with both book1 and book2 instances.
Singleton Scope
SpringConfig.xml
In our configuration file we have defined the scope as “singleton”
<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="book" class="com.javainterviewpoint.Book" scope="singleton"></bean> </beans>
Output :
When we run our ClientLogic class, we will be getting the below output
**Published book1 Details** Book Title : Core Spring Book Author : Java Interview Point Book Publications : JIP **Published book2 Details** Book Title : Core Spring Book Author : Java Interview Point Book Publications : JIP
Even though we didn’t set values for book2 instance we will get the values which is set for book1, as singleton scope is single bean instance per Spring IoC container. It is something like static variable in java
Prototype Scope
SpringConfig.xml
We will define scope as “prototype” from Book bean
<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="book" class="com.javainterviewpoint.Book" scope="prototype"></bean> </beans>
Output :
When we run our ClientLogic class, we will be getting the below output
**Published book1 Details** Book Title : Core Spring Book Author : Java Interview Point Book Publications : JIP **Published book2 Details** Book Title : null Book Author : null Book Publications : null
Here we got book2 details as null as we have used prototype scope, which creates new bean instance every time.
Leave a Reply