0

According to me only one bean should be created since i have given the scope as singleton but output is saying different thing. Can anyone please ellaborate the following to me please,

HelloWorld.java

public class HelloWorld {
    private String message;

    public HelloWorld(String message) {
        System.out.println(message+ "bean created");
        this.message=message;
    }

    public void getMessage() {
        System.out.println("Your Message : " + message);
   }
}

Main.java

public class Main {
    public  static void main(String args[]) {

        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
        obj.getMessage();

        HelloWorld obj2 = (HelloWorld) context.getBean("helloWorld2");
        obj2.getMessage();
        obj.getMessage();

        System.out.println(obj.hashCode());
        System.out.println(obj.hashCode());
    }
}

beans.xml

<bean id = "helloWorld" class = "HelloWorld" scope="singleton">
    <!--<property name = "message" value = "Hello World!"/>-->
    <constructor-arg value="HelloWorld1"/>
</bean>

<bean id = "helloWorld2" class = "HelloWorld" scope="singleton">
    <!--<property name = "message" value = "Hello World2!"/>-->
    <constructor-arg value="HelloWorld2"/>
</bean>

Output:

HelloWorld1bean created
HelloWorld2bean created
Your Message : HelloWorld1
Your Message : HelloWorld2
Your Message : HelloWorld1
935148943
935148943
Max
  • 915
  • 10
  • 28
Ishant Gaurav
  • 1,183
  • 2
  • 13
  • 32
  • 1
    Possible duplicate of [Singleton design pattern vs Singleton beans in Spring container](https://stackoverflow.com/questions/2637864/singleton-design-pattern-vs-singleton-beans-in-spring-container) – alfcope Jul 28 '17 at 12:02
  • This is different, I know the difference between spring singleton and java singleton, but in this case i have created bean singleton still 2 beans are being created . Please help me what i m missing here as per the output. – Ishant Gaurav Jul 28 '17 at 12:19
  • 1
    You do not know the difference if you are asking this question. You are confusing between singleton class (a class you can only create a object from) and a singleton bean. The beans are singleton. Once you have created the "`helloWorld`" bean you are going to get always the same instance (within the same context) for that bean, but if you try to create a "`helloWorld2`", using the same class, of course you are going to get a different object. They have different id and you have probably used different constructor parameters. You can autowire any of them using `@Qualifier`. – alfcope Jul 28 '17 at 15:24
  • Yeaah seems like i was actually confused . Thanks for the help. Appreciate it – Ishant Gaurav Jul 28 '17 at 16:31

2 Answers2

1

Definitely 2 Try to print obj2.hashcode. Not obj hashcode

0

No here 2 singleton beans will create becaue you are creating 2 different beans for HelloWorld

<bean id = "helloWorld" class = "HelloWorld" scope="singleton">
    <!--<property name = "message" value = "Hello World!"/>-->
    <constructor-arg value="HelloWorld1"/>
</bean>

<bean id = "helloWorld2" class = "HelloWorld" scope="singleton">
    <!--<property name = "message" value = "Hello World2!"/>-->
    <constructor-arg value="HelloWorld2"/>
</bean>

Spring container create 1 object per definition, If we define bean N times than N singleton object of the class will be created

Bhushan Uniyal
  • 5,575
  • 2
  • 22
  • 45
  • If we can create more than one bean then what is the significance of creating singleton . I am still confused . – Ishant Gaurav Jul 28 '17 at 13:27
  • Here Two seperate instances will be created. Yes this is not a singleton anymore in a classical meaning (one instance per JVM) - (if ever was), however the created bean (each of them) has a singleton scope (in a Spring meaning). If you really want to assure that an object of a given class will be always a singleton (only one instance per JVM) see Correct way of making a singleton a Spring bean ->"https://stackoverflow.com/questions/6205171/correct-way-of-making-a-singleton-a-spring-bean". – Bhushan Uniyal Jul 28 '17 at 15:56
  • But the question is if you really need 'the real singleton'?! see->"http://docs.spring.io/spring/docs/3.2.1.RELEASE/spring-framework-reference/html/beans.html#beans-factory-scopes" – Bhushan Uniyal Jul 28 '17 at 15:56
  • I was really confused , thanks for the help . Appreciate it. – Ishant Gaurav Jul 28 '17 at 16:29
  • happy to help :) – Bhushan Uniyal Jul 28 '17 at 16:40