It's not a secret anymore!*
The moose likes Spring and the fly likes When are Beans in scopes other than singleton exactly loaded? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "When are Beans in scopes other than singleton exactly loaded?" Watch "When are Beans in scopes other than singleton exactly loaded?" New topic
Author

When are Beans in scopes other than singleton exactly loaded?

Sumeet H Singh
Greenhorn

Joined: Jan 03, 2009
Posts: 29
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)?


Sumeet
SCJP, SCWCD
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
joy b chakravarty
Ranch Hand

Joined: May 16, 2011
Posts: 62

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:
<beans default-lazy-init="true">
<!-- no beans will be pre-instantiated... -->
</beans>


Cheers, Joy [SCJP 1.4, SCBCD 5.0]
get high on alcohol, algorithm or both
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When are Beans in scopes other than singleton exactly loaded?
 
Similar Threads
Lazy and preloading of beans
How to create NON SINGLETON beans in Spring ?
Not able to test AOP using AspectJ
Common Spring context for all EJBs
Spring application context