【问题标题】:what is the Difference Between singleton and prototype scopes in Spring 2.5Spring 2.5中单例和原型范围之间的区别是什么
【发布时间】:2013-01-30 05:32:00
【问题描述】:

我阅读了关于 spring bean 范围的教程,他们提到如果 bean 范围是 prototype Spring 容器将在每个 context.getBean("id") 语句中创建新对象。如果我们指定范围是singleton,即使我们写context.getBean("id") 语句两次或更多次,它也只会创建一个对象...... 我做了一个小例子

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");  
                   }

        }
}

上面的程序打印

 P hashCode :18303751
 Q hashCode :18303751
 Same instance!

但在Person bean 范围内,我给出了scope="prototype" 为什么它打印相同的哈希码????

解释任何人...

在此先感谢...

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();

    }


}

【问题讨论】:

  • 不应以任何方式返回两个相同的对象。我刚刚执行了你的代码(假设 person 是一个简单的 POJO 类,属性为'name'),我得到了两个不同的对象!您是否在 Person 中覆盖了 hashcode() 以返回类似 name.hashCode() 的内容?
  • 我刚试了你的最新代码,得到:P hashCode :763970039 Q hashCode :239720060 不同实例
  • @Jintian DENG : 是 Jar 文件的问题...之前我放置了旧的 jar 文件,后来我用新的 jar 检查了它的工作正常......

标签: spring inversion-of-control ioc-container


【解决方案1】:

这是因为您在 person 模型中覆盖了 hashCode

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

你执行:

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

twise 但对于 p 和 q ,name 属性具有相同的值,即 “Hello World”。所以它打印相同的 hashCode。

因此,如果不覆盖 hashCode() ,输出会有所不同。 p 和 q 会得到不同的 hashCode。

编辑:试试

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

在您的代码中,您会看到单例和原型之间的区别。

【讨论】:

  • 并且hashcode of String是根据它的字符计算的,而不是使用默认的对象hashcode,相同字符串的两个副本具有相同的hashcode。
  • @vertti:没错。对于相同的字符串,hashCode 显然是相同的。 OP 应该尝试使用 super.hashCode() 来查看实际差异。
【解决方案2】:

正如另一个答案所建议的,您已经覆盖了hashCode 方法,它是根据name 字符串计算的。

您不应该使用哈希码来验证两个引用是否指向同一个对象实例。直接比较它们:

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");
}

【讨论】:

  • 是的,我按照你说的更改了代码,但它仍然打印相同的输出..我编辑了我的代码,见一次......
【解决方案3】:

问题在于 Jar 文件...之前我使用 Spring 2.5 jar,它会打印相同的 hashCode 但后来我更改了 jar,我添加了 Spring 3.0 相关的 jar,现在我得到了正确的输出..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-19
    • 2013-04-10
    • 2012-04-16
    • 1970-01-01
    • 2013-12-01
    • 2015-06-19
    • 2012-03-28
    相关资源
    最近更新 更多