1

i read tutorial about spring bean scopes in that they mentioned if bean scope is prototype Spring container will create new object every context.getBean("id") statement. and if we specify scope is singleton it will create only one object even though we write context.getBean("id") statement two times or more... i did small example

Demo.java

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

          ApplicationContext con=new ClassPathXmlApplicationContext("spconfig.xml");

          Person p=(Person)con.getBean("person");
          Person q=(Person)con.getBean("person");
          System.out.println(" P hashCode :"+p.hashCode());
          System.out.println(" Q hashCode :"+q.hashCode());
              if (p==q)
              {  
                 System.out.println("Same instance!");
              } 
              else {
              System.out.println("Different instance");  
                   }

        }
}

the above program prints

 P hashCode :18303751
 Q hashCode :18303751
 Same instance!

But in Person bean scope i given scope="prototype" why it is printing same Hashcode ????

Explain anyone...

Thanks in Advance...

spconfig.xml

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
            "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
    <bean id="person" class="demo.Person" scope="prototype">
       <property name="name" value="Hello World" />
    </bean>  
</beans>   

Person.java

package demo;

public class Person {
private String name;


public String getName() {
    return name;
}


public void setName(String name) {
    this.name = name;
}
public void printHello() {
    System.out.println("Hello ! " + name);
}

@Override
public int hashCode() {
 return super.hashCode();

    }


}
Jintian DENG
  • 3,152
  • 20
  • 22
Clarence
  • 896
  • 1
  • 9
  • 27
  • There should not be two same objects returned in any way. I just executed your peice of code (Assuming person to be a simple POJO class with an attribute 'name') and I got two different objects !! Have you overridden hashcode() within Person to return something like name.hashCode() ? – Anugoonj Jan 30 '13 at 06:45
  • I just tried your newest code and got: P hashCode :763970039 Q hashCode :239720060 Different instance – Jintian DENG Jan 30 '13 at 11:04
  • @ Jintian DENG : Ya problem with Jar files... earlier i placed old jar files later i checked with New jars its working fine.... – Clarence Jan 30 '13 at 13:15

3 Answers3

0

It is because you have overridden hashCode in your person model.

@Override
public int hashCode() {
  return name.hashCode();

You execute:

Person p=(Person)con.getBean("person");
Person q=(Person)con.getBean("person");

twise but for both p and q , name property have same value i.e. "Hello World". So it prints same hashCode.

So, without overriding hashCode() , output will be different. You will get different hashCode for both p and q.

EDIT: Try

@Override
public int hashCode() {
  return super.hashCode();

In your code , and you'll see the difference between singleton and prototype.

Priyank Doshi
  • 12,895
  • 18
  • 59
  • 82
  • And the [hashcode of String](http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#hashCode%28%29) is calculated based on it's characters, not with the default object hashcode, two copies of the same string have equal hashcodes. – vertti Jan 30 '13 at 07:07
  • @vertti : yeah exactly . for same string, hashCode would obviously be same. OP should try with super.hashCode() to see actual difference. – Priyank Doshi Jan 30 '13 at 07:08
0

As another answer suggested, you have overridden the hashCode method, and it is calculated base on the name string.

You shouldn't use hash code to verify whether two references are pointing to same object instance. Compare them directly instead:

Person p=(Person)con.getBean("person");
Person q=(Person)con.getBean("person");
if (p==q) {
    System.out.println("Same instance!");
} else {
    System.out.println("Different instance");
}
Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
  • Yeah i changed the code as you said but it still printing same output.. i edited my code see once... – Clarence Jan 30 '13 at 07:22
0

Problem is with Jar file... in earlier i used Spring 2.5 jars it will print same hashCode but later i changed jars, i added Spring 3.0 related jars now i'm getting Correct out put..

Clarence
  • 896
  • 1
  • 9
  • 27