PDA

View Full Version : Why aren't my singletons instanciated?



susie_derkins
Sep 8th, 2004, 05:29 PM
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:


public class Foo {
public Foo() {System.out.println("Foo.Foo");}
}


The definition file:


<?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:


InputStream is;
is = ClassLoader.getSystemClassLoader&#40;&#41;.getResourceAsSt ream&#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:


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.

irbouho
Sep 8th, 2004, 10:13 PM
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:


FileSystemXmlApplicationContext ctx = new FileSystemXmlApplicationContext&#40;"foo.xml"&#41;;

Colin Sampaleanu
Sep 9th, 2004, 03:49 PM
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.

susie_derkins
Sep 9th, 2004, 03:49 PM
Thanks Omar! That worked great.

Cheers,

Susie.

jbetancourt
Sep 9th, 2004, 04:29 PM
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.

Colin Sampaleanu
Sep 9th, 2004, 04:46 PM
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.