All singleton beans loaded using ApplicationContext are loaded when the spring context is loaded...and singleton beans loaded using BeanFactory are loaded only when getBean() method is called...
What about beans in scopes other than singleton ?
Does ApplicationContext load all the beans(with any scope) while initializing context?
And does BeanFactory always lazily load the beans(irrespective of scope)?
Can't speak for BeanFactory since I have always used ApplicationContext.
Main thing is that the beans get created at the right time in the scope. So for session scope in a web environment, the first time the user starts a session and calls getBean, then the instance is created, and every further request by that user within that users http session, they will then get that same instance.
Hope the excerpt helps..
By default, ApplicationContext implementations eagerly create and configure all singleton beans as part of the initialization process. Generally, this pre-instantiation is desirable, because errors in the configuration or surrounding environment are discovered immediately, as opposed to hours or even days later. When this behavior is not desirable, you can prevent pre-instantiation of a singleton bean by marking the bean definition as lazy-initialized. A lazy-initialized bean tells the IoC container to create a bean instance when it is first requested, rather than at startup.
In XML, this behavior is controlled by the lazy-init attribute on the <bean/> element; for example:
<bean id="lazy" class="com.foo.ExpensiveToCreateBean" lazy-init="true"/>
<bean name="not.lazy" class="com.foo.AnotherBean"/>
When the preceding configuration is consumed by an ApplicationContext, the bean named lazy is not eagerly pre-instantiated when the ApplicationContext is starting up, whereas the not.lazy bean is eagerly pre-instantiated.
However, when a lazy-initialized bean is a dependency of a singleton bean that is not lazy-initialized, the ApplicationContext creates the lazy-initialized bean at startup, because it must satisfy the singleton's dependencies. The lazy-initialized bean is injected into a singleton bean elsewhere that is not lazy-initialized.
You can also control lazy-initialization at the container level by using the default-lazy-init attribute on the <beans/> element; for example:
<!-- no beans will be pre-instantiated... -->
Cheers, Joy [SCJP 1.4, SCBCD 5.0]
get high on alcohol, algorithm or both
Evacuate the building! Here, take this tiny ad with you: