Spring Singleton and Prototype

Concept Overview

In Dependency injection, it is the responsibility of the container (e.g. Spring framework) to instantiate the objects (a.k.a.
in Spring terminology). Spring can instance objects in two ways:

  1. Singleton: Instantiate only one object
  2. Prototype: Instantiate a new object everytime

In Spring terminology,
define the
of the bean.

e.g. There is usually one instance of Ticket Vending Machine (usually available in underground metro railway stations). However, a Ticket Vending Machine generates a new instance on Ticket everytime. In this case, Ticket Vending Machine bean has scope
while Ticket bean has scope

Sample Program Overview

In the sample program, we define two classes: TicketVendingMachine and Ticket. We shall declare TicketVendingMachine as Singleton and Ticket and Prototype.

We shall fetch two references of TicketVendingMachine from Spring and verify that both the references point to the same instance of TicketVendingMachine because TicketVendingMachine bean is defined as Singleton.

Then we shall fetch two references of Ticket from Spring and verify that both the references point to the different instances of Ticket because Ticket bean is defined as Prototype.

Required Libraries
  • log4j.jar
  • org.springframework.core.jar
  • org.springframework.context.jar
  • org.springframework.context.support.jar
  • org.springframework.beans.jar
  • commons-logging.jar
  • org.springframework.aop.jar
  • org.springframework.asm.jar
  • org.springframework.expression.jar

Source Code

Create TicketVendingMachine class

Create Ticket class

Declare the TicketVendingMachine and Ticket classes in spring-config.xml (see line 10 and line 14 below).

TicketVendingMachine is declared explicitly with scope Singleton (see line 12 below). This is the default scope of all beans in Spring.i.e. even if
is not mentioned explicitly, still the TicketVendingMachine bean will have the scope as Singleton by default.

Ticket is declared with scope Prototype (see line 16 below)

Finally, we need a java program to test the Singleton and Prototype setup.This is done by TestSingletonAndPrototype.java (see source code below)

We need to tell Spring framework to use the ‘spring-config.xml’ to load our beans(see line 11 below)

We get two references of TicketVendingMachine bean (see lines 13-14 below)

We then compare the two instances and print the result of the comparison (see lines 15-16 below). The result of this comparison should return
indicating the two references point to the same instance because TicketVendingMachine is declared as Singleton.

We get two references of Ticket bean (see lines 18-19 below)

We then compare the two instances and print the result of the comparison (see lines20-21 below). The result of this comparison should return
indicating that the two references point to different instances because Ticket is declared as Prototype.

Running Sample Program

This sample program has been packaged as a jar installer which will copy the source code (along with all necessary dependencies) on your machine and automatically run the program for you as shown in the steps below. To run the sample program, you only need Java Runtime Environment (JRE) on your machine and nothing else.

Download And Automatically Run Sample Program

  • Save the springsetterinjection-installer.jar on your machine
  • Execute/Run the jar using Java Runtime Environment

  • (Alternatively you can go the folder containing the springsetterinjection-installer.jar and execute the jar using
    java -jar springsetterinjection-installer.jar

  • You will see a wizard as shown below

  • Enter the location of the directory where you want the program to install and run (say, C:\Temp)

  • The installer will copy the program on your machine and automatically execute it as shown below. The following outputs
      Are ticket vending machine instances same : true
      Are ticket instances same : false

    indicate that the program has run successfully on your machine

  • Browsing the Program

    This source code for this program is downloaded in the folder specified by you (say, C:\Temp) as an eclipse project called
    . All the required libraries have also been downloaded and placed in the same location. You can open this project from Eclipe IDE and directly browse the source code. See below for details of the project structure.

    Leave a Reply

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