Announcement Announcement Module
Collapse
No announcement yet.
Problems with prototype scope: Same bean! Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problems with prototype scope: Same bean!

    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:
    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);
            }
        }
    }
    prototype-beans.xml:
    HTML 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>
    Whenever I run this I get something like
    HTML Code:
    Objects are the same: [2268884] == [2268884]

  • #2
    Guess, you have 2 your prototypes-beans.xml twice in the classpath - once with prototype and once without.

    I have copied your sample as is, without any modifications and it works as it should
    Code:
    28.07.2007 21:36:14 org.springframework.context.support.AbstractApplicationContext prepareRefresh
    INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@109a4c: display name [org.springframework.context.support.ClassPathXmlApplicationContext@109a4c]; startup date [Sat Jul 28 21:36:14 CEST 2007]; root of context hierarchy
    28.07.2007 21:36:15 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from class path resource [prototype-beans.xml]
    28.07.2007 21:36:15 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
    INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@109a4c]: org.springframework.beans.factory.support.DefaultListableBeanFactory@10ef90c
    28.07.2007 21:36:15 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
    INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@10ef90c: defining beans [test]; root of factory hierarchy
    Objects are different: [17005681] != [31393597]
    I have tested against several Spring versions.

    Comment


    • #3
      This turns out to be a classpath problem: There was an old version of Spring that was being picked up, which ignored the scope attribute (I assume).

      Comment


      • #4
        Originally posted by philion View Post
        This turns out to be a classpath problem: There was an old version of Spring that was being picked up, which ignored the scope attribute (I assume).
        How old? 1.x?

        Comment

        Working...
        X