Results 1 to 6 of 6

Thread: Why aren't my singletons instanciated?

  1. #1

    Default 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. #2
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    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;;
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  3. #3
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    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.
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

  4. #4

    Default

    Thanks Omar! That worked great.

    Cheers,

    Susie.

  5. #5
    Join Date
    Aug 2004
    Location
    u.s.a
    Posts
    399

    Default

    Quote 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.

  6. #6
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    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.

Similar Threads

  1. Replies: 8
    Last Post: Jun 26th, 2005, 11:05 PM
  2. not so Singletons, and Cyclic references
    By Ian Johnson in forum Container
    Replies: 0
    Last Post: May 23rd, 2005, 04:18 PM
  3. Replies: 5
    Last Post: Apr 27th, 2005, 10:52 AM
  4. Multiple Singletons ?
    By aloleary in forum Container
    Replies: 4
    Last Post: Apr 20th, 2005, 08:58 AM
  5. Replies: 1
    Last Post: Jan 11th, 2005, 12:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •