I am trying to use the prototype scope in Spring 2.0.5 to create unique beans, but I keep getting the same bean.
I have reduced it to the simplest code I can to repro the problem:
TestBean.java:
prototype-beans.xml:HTML Code:import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestBean { private final String val; private String prop; public TestBean() { this.val = "default"; } public TestBean(String value) { this.val = value; } public void setSomeProperty(String value) { this.prop = value; } public String toString() { return "[" + System.identityHashCode(this) + "]"; } public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("classpath:prototype-beans.xml"); TestBean bean1 = (TestBean) context.getBean("test"); TestBean bean2 = (TestBean) context.getBean("test"); int id1 = System.identityHashCode(bean1); int id2 = System.identityHashCode(bean2); if (id1 == id2) { System.out.println("Objects are the same: " + bean1 + " == " + bean2); } else { System.out.println("Objects are different: " + bean1 + " != " + bean2); } } }
Whenever I run this I get something likeHTML Code:<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans> <bean id="test" class="TestBean" scope="prototype"/> </beans>
HTML Code:Objects are the same: [2268884] == [2268884]


Reply With Quote
