0

In simple words Singleton in Java means only one instance of a class in JVM. Whereas in Spring it is one instance of a class in the Spring container (reason is, single JVM can contain multiple Spring Container).

In Java, when we implement a Singleton class we ensure that every-time the one and only instance of the class be returned, whenever accessed. This can be achieved by the below program.

public final class MySingleton {

    private static MySingleton mySingleton = new MySingleton();

    private MySingleton() {
    }

    public static MySingleton getInstance() {
        return mySingleton;
    }
}

In case of spring if we define two beans of the same class we get two instance in our Java application when those beans are accessed.

<bean id="employee1" class="test.demo.Employee" scope="singleton"></bean>
<bean id="employee2" class="test.demo.Employee" scope="singleton"></bean>

It is true that, context.getBean("employee2"); always returns the same instance. At the same time, the default scope in Spring is Singleton. My question is, doesn't the above code defies the purpose of Singleton?

Even if we define scope="singleton" in multiple bean declaration of the same class why does Spring always return a new instance?

Sudipta Roy
  • 103
  • 1
  • 3
  • 11
  • 3
    I guess http://stackoverflow.com/questions/31629993/is-spring-default-scope-singleton-or-not is what you were looking for. – Unknown Aug 31 '16 at 06:19
  • In spring, it is not one instance of class. It is one instance of bean defined with id. For one id, it is always one bean. – kswaughs Aug 31 '16 at 06:20
  • @Unknown The link you shared has a partial answer. I wanted to know that if in Java the same instance could be returned using the program, then why Spring returns multiple instance. – Sudipta Roy Aug 31 '16 at 06:22
  • Please go through this tutorial for more details http://javabeat.net/spring-singleton-java-singleton/ – Maheshwar Ligade Aug 31 '16 at 06:24
  • @kswaughs The Singleton's purpose is to limit the number of objects to one only. So if we look at the Spring behavior from the Java perspective isn't the concept of Singleton defied? – Sudipta Roy Aug 31 '16 at 06:26
  • @MaheshwarLigade Please read the first three lines of my question. There I have already mentioned that for Java it is single instance in JVM and for Spring it is single instance in Container. The link you shared nowhere explains multiple beans of the same class. – Sudipta Roy Aug 31 '16 at 06:30
  • @Unknown The last two lines of the accepted answer says _"Spring would consider something a singleton if it cannot create more than one instance of a class within a given container/context."_ But we see that indeed multiple instances are being created. Also one comment says _Even if you declare same bean two different times, it will be created just once._ I doubt if it is correct. Could you please explain if it defies the purpose of Singleton? – Sudipta Roy Aug 31 '16 at 07:22
  • 2
    The bean is an instances not class instances! you can have multiple beans for the same class (and differently configured) the bean will be created once and only once. Also your perception of the singleton in plain java is wrong it isn't a single instance for the JVM it is an instance per classloader! If 2 classloaders would load the `MySingleton` class you will effectivly have 2 isntances in the JVM. – M. Deinum Aug 31 '16 at 07:30
  • When we define a singleton class, we delivery an information that the design purpose of this class is designed for Singleton purpose. But we can not control the users how to use. The user still can create a lot of instances through reflection. Whatever in Class, we define as an singleton or in Spring, we scope the bean as singleton. We just go to tell the user what is the purpose of this design. just one instance will be used in their context. – Eric Aug 31 '16 at 07:32
  • @M.Deinum Thank you very much for pointing out my wrong understanding regarding Java. I guess in case of Spring, in the same container multiple beans of the same class can be present and each one of them are treated as Singleton with that particular configuration. – Sudipta Roy Aug 31 '16 at 07:36

0 Answers0