Announcement Announcement Module
Collapse
No announcement yet.
Why aren't my singletons instanciated? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Why aren't my singletons instanciated?

    Springers,

    It was my understanding that the BeanFactory instanciates the beans defined in the config file, providing lazy-init is set to ďfalseĒ and the bean is a singleton.

    Now, it appears that singleton are only created by a call to factory.get Bean. Is that the right behavior or Am I missing something?

    Here is a code sample:

    The bean:
    Code:
    public class Foo {
       public Foo() {System.out.println("Foo.Foo");}
    }
    The definition file:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
    <beans default-lazy-init="false">
      <bean id="foo" class=" Foo">
      </bean>
    </beans>
    I load this file with the following code:
    Code:
    InputStream is;
       is =  ClassLoader.getSystemClassLoader&#40;&#41;.getResourceAsStream&#40;"foo.xml"&#41;;
       XmlBeanFactory factory = new XmlBeanFactory&#40;is&#41;;

    I would expect that there exist an instance of Foo after the call to new XmlBeanFactory. Itís apparently not the case (I donít see the constructorís trace in the console).

    The log file is as follow:
    Code:
    DefaultXmlBeanDefinitionParser -- Loading bean definitions 
     DefaultXmlBeanDefinitionParser -- Default lazy init 'false' 
    DefaultXmlBeanDefinitionParser -- Default dependency check 'none' 
    DefaultXmlBeanDefinitionParser -- Default autowire 'no' 
    DefaultXmlBeanDefinitionParser -- Registering bean definition with id 'foo' 
    DefaultXmlBeanDefinitionParser -- Found 1 <bean> elements defining beans
    Is something wrong with this code?

    Thanks,

    Susie.

  • #2
    The singleton is not pre-created because preInstantiateSingletons is not called when XmlBeanFactory is instanciated. You can either call it manually or use an ApplicationContext to load your file:
    Code:
      FileSystemXmlApplicationContext ctx = new FileSystemXmlApplicationContext&#40;"foo.xml"&#41;;

    Comment


    • #3
      To clarify Omar's message a bit, BeanFactory variants don't pre-instantiate singletons unless you call preInstantiateSingletons, while ApplicationContext variangts do that for you.

      Comment


      • #4
        Thanks Omar! That worked great.

        Cheers,

        Susie.

        Comment


        • #5
          Originally posted by Colin Sampaleanu
          To clarify Omar's message a bit, BeanFactory variants don't pre-instantiate singletons unless you call preInstantiateSingletons, while ApplicationContext variangts do that for you.
          Is there any short reason why they work differently? I'm guessing its in line with the "...the context package adds ApplicationContext, which enhances BeanFactory functionality in a more framework-oriented style. "

          Also, if lazy-init is set to 'false', shouldn't they? Or this be made explicity in the reference docs.

          Comment


          • #6
            I think this stems from the 'programmatic' typical usage of the BeanFactory, vs. the higher level and often declarative usage of the AppContext.

            As for the lazy-init flag, if it's on, then it overrides the pre-instantiation, and the bean will only be initialized when actually needed.

            Comment

            Working...
            X