Spring BeanFactory Singleton Prototype Objects


BeanFactory Singleton Prototype Examples Screenshots Simple terms given

In singleton bean, only one object is created irrespective of any number of objects client creates. In prototype bean, multiple objects are created. These are illustrated in this application. Two client programs are written with the same output but using BeanFactory and applicationContext.

Note: Both BeanFactory and ApplicationContext are containers. For their difference, refer containers in Introduction to Java Spring Framework.

BeanFactory Singleton Prototype

1. Bean Program: A.java

public class A 
{
  String name;
  public A(String n) 
  {
    name = n;
    System.out.println("A (name) constructor called. Name is assigned a value.");
  }
  public A() 
  {
    System.out.println("A() default constructor called");
  }
  public void setA(String n) 
  {
    System.out.println("setA(name) method called");
    name = n;
  }
  public void start() 
  {
    System.out.println("start() method called");
  }
  public void print() 
  {
    System.out.println(name);
  }
}

2. Dependency XML file: applicationContext.xml





   



   
  
 

Observe the scope attribute. Singleton means only one object is created irrespective of any number calls, the client makes. But it prototype, for each object called by client, different bean objects are created.

<property name="a" value="Trainer"/>

name="a" calls setA() method.

init-method="start"

A bean starts its life-cycle events with init() method. Spring framework provides facility to initialize the Beans using the user defined methods in place of actual life cycle methods. The user defined method is written in init-method attribute. In the above code, the init() method invokes the user defined start().

After constructors, the program execution starts with start() method.

3. First Client Program: Client1.java (Using BeanFactory)

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;

public class Client1 
{
  public static void main(String rags[]) throws Exception 
  {
    BeanFactory bf = new XmlBeanFactory(new FileSystemResource( "C:\\snr\\applicationContext.xml"));

    System.out.println("a1 object is SINGLETON");
    System.out.println("a1 object assigned to a1 object info:");
    A a1 = (A) bf.getBean("a1");
    a1.print(); 
    System.out.println(a1);
    System.out.println("a1 object assigned to a2 object info:");
    
    A a2 = (A) bf.getBean("a1");
    a2.print(); 
    System.out.println(a2);
    System.out.println("\na2 object is PROTOTYPE");
    System.out.println("a2 object assigned to a3 object info:");
    
    A a3 = (A) bf.getBean("a2");
    a3.print(); 
    System.out.println(a3);
    System.out.println("a2 object assigned to a4 object info:");
    
    A a4 = (A) bf.getBean("a2");
    a4.print(); 
    System.out.println(a4);
  }
}

Generally, the applicationContext.xml file is placed in src folder of MyEclipse. Infact, it can be any folder on the file system. In the above client program, the XML file is placed in C:\snr folder.

Console screenshot when Client1 is run (of BeanFactory Singleton Prototype):

BeanFactory Singleton Prototype

As a1 object is singleton, both times the same object is returned (it printed [email protected]).

As a2 object is prototype (means not singleton), two times two different objects are returned (it printed [email protected] and [email protected])

4. Second Client Program: Client2.java (Using ApplicationContext)

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

public class Client2 
{
  public static void main(String rags[]) throws Exception 
  {
    ApplicationContext bf = new 
ClassPathXmlApplicationContext("applicationContext.xml")
    System.out.println("a1 object is SINGLETON");
    System.out.println("a1 object assigned to a1 object info:");

    A a1 = (A) bf.getBean("a1");
    a1.print(); 
    System.out.println(a1);
    System.out.println("a1 object assigned to a2 object info:");

    A a2 = (A) bf.getBean("a1");
    a2.print(); 
    System.out.println(a2);
    System.out.println("\na2 object is PROTOTYPE");
    System.out.println("a2 object assigned to a3 object info:");

    A a3 = (A) bf.getBean("a2");
    a3.print(); 
    System.out.println(a3);
    System.out.println("a2 object assigned to a4 object info:");

    A a4 = (A) bf.getBean("a2");
    a4.print(); 
    System.out.println(a4);
  }    
}  

Console screenshot when Client2 is run (of BeanFactory Singleton Prototype):

BeanFactory Singleton Prototype

The output of both the programs is same but they differ in the number of objects created in the program. Sometime, singleton may be advantageous for performance.

Pass your comments for the improvement of this tutorial "Spring BeanFactory Singleton Prototype Objects".

Leave a Comment

Your email address will not be published.