I am learning Spring and I have a question regarding the preInstantiateSingletons method on the bean factory. In what way does Spring lazy-load singletons if you don’t call this method? I am reading Pro Spring 2.5 and use the XmlBeanFactory. I thought Spring made the whole object graph at the point you were calling the constructor.
And yes I know it is deprecated…
By default Spring has eager bean initialization behavior, but if you mark the bean to initialize lazily by
lazy-init="true"or may be with annotation spring context while loading the context will not create the instance of that bean, It will create the instance when we actually need it(When we try to extract that bean from context, or when that bean is marked as dependency in other bean and that bean is under initialization)To test and validate this you could create a bean and mark it as lazy initialization and another bean with no lazy initialization (default eager), Put the log statement in both of them’s constructor the BeanEager’s constructor would get invoked as soon as contextt gets loaded but BeanLazy’s constructor would get invoked only when you try to actually fetch that bean from context