Home > Java > Singleton vs Prototype Spring injection

Singleton vs Prototype Spring injection

For a newbie developer that just started their journey in the Spring auto injection world. I will explain about the differences Singleton and Protoype Spring Injection.

To make it easier i will create a simple POJO Java Class that only contain one property and its setter getter.


package com.namex.spring;

public class NamexBean {
 private String myMessage;

public String getMyMessage() {
 return myMessage;
 }

public void setMyMessage(String myMessage) {
 this.myMessage = myMessage;
 }

}

SINGLETON

And for the first, we will advancing the singleton scope.


<beans xmlns="<a href="http://www.springframework.org/schema/beans">http://www.springframework.org/schema/beans</a>"
 xmlns:xsi="<a href="http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance</a>"
 xsi:schemaLocation="<a href="http://www.springframework.org/schema/beans">http://www.springframework.org/schema/beans</a>

<a href="http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">http://www.springframework.org/schema/beans/spring-beans-2.5.xsd</a>">

<bean id="namexBean" class="com.namex.spring.NamexBean" scope="singleton" />

</beans>

By default, spring has its scope as a “singleton”, so you if you not specifiying the scope attribute, it will be the “singleton”.

Now, i will create my java tester class, to start to test the effect of the singleton scope.


package com.namex.spring;

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

public class Tester {
 public static void main(String[] args) {
 ApplicationContext context = new ClassPathXmlApplicationContext(
 new String[] { "Spring-Context.xml" });

NamexBean firstBean = (NamexBean) context.getBean("namexBean");
 firstBean.setMyMessage("This is the first bean's message");
 System.out.println("firstBean: " + firstBean.getMyMessage());

NamexBean secondBean = (NamexBean) context.getBean("namexBean");
 System.out.println("secondBean: " +secondBean.getMyMessage());
 }
 }

the output would be:


firstBean: This is the first bean's message

secondBean: This is the first bean's message

Now, it’s clear, with the singleton scope, spring will create one instance that will be shared across among others. So they will be have the same memory reference.

PROTOTYPE

Now how about with the Prototype?


<beans xmlns="<a href="http://www.springframework.org/schema/beans">http://www.springframework.org/schema/beans</a>"
 xmlns:xsi="<a href="http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance</a>"
 xsi:schemaLocation="<a href="http://www.springframework.org/schema/beans">http://www.springframework.org/schema/beans</a>

<a href="http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">http://www.springframework.org/schema/beans/spring-beans-2.5.xsd</a>">

<bean id="namexBean" class="com.namex.spring.NamexBean" scope="prototype" />

</beans>


package com.namex.spring;

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

public class Tester {
 public static void main(String[] args) {
 ApplicationContext context = new ClassPathXmlApplicationContext(
 new String[] { "Spring-Context.xml" });

NamexBean firstBean = (NamexBean) context.getBean("namexBean");
 firstBean.setMyMessage("This is the first bean's message");
 System.out.println("firstBean: " + firstBean.getMyMessage());

NamexBean secondBean = (NamexBean) context.getBean("namexBean");
 System.out.println("secondBean: " +secondBean.getMyMessage());
 }
 }

the output would be:


firstBean: This is the first bean's message

secondBean: null

It’s obvious that the Prototype scope create a new instance and not shared the instance among.

WHICH ONE IS BETTER ?

The answer is really simple: it’s depend. Depend in which way you need the code to do and how you code it.

 

Advertisements
Categories: Java Tags: , ,
  1. Praveen Govindan
    5 January 2011 at 06:30

    I don’t think there is any performance penalty in using a singleton pattern. Could you explain what you mean by “But the process will take faster in time because it has no needs to wait the previous to be finished first”? This will be the issue only if your business method in the spring managed bean is synchronized. For non-synchronized methods, there is no penalty. In fact there could be a penalty for prototype, since every invocation has to create a new instance.

    Praveen

  2. Shailendra kumar
    5 January 2011 at 11:22

    Well the only reason you would choose one over the other will be based on your bean design. If your bean is acting as some sort of service and has no state ( or immutable state), then you can safely make that as singleton. Also the lifecycle is managed by Spring in case of singleton bean. For prototype bean you , Spring simply hand them over to you and its upto you to manage those. So its simply how you are going to use the beans which will decide whether to make singleton or prototype.

  3. Arun
    5 January 2011 at 13:03

    Is it true that in case of singleton usage the process will wait till the other process/ thread releases the object?. I think we can get the copy of the object at that instance from spring context and proceed.

  4. RDeJourney
    5 January 2011 at 13:49

    Gosh, i made a mistake, thanx for Praveen, i’ll made correction of this blog after i got my free time.

    @Arun: Nope, i made a mistake, Praveen and Shailendra is right about this.

    Guys, may i have your blog link? or personal messenger? if you don’t mind. 🙂

  1. 3 January 2011 at 17:56

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: