aspose file tools*
The moose likes Spring and the fly likes Lazy and preloading of beans Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Lazy and preloading of beans" Watch "Lazy and preloading of beans" New topic
Author

Lazy and preloading of beans

Sudhanshu Mishra
Ranch Hand

Joined: May 28, 2011
Posts: 217

Hi all,
Can someone please explain my question according to following paragraph in "Spring in action"
"another big difference between an application context and a bean factory is how
singleton beans are loaded. A bean factory lazily loads all beans, deferring bean
creation until the getBean() method is called. An application context is a bit
smarter and preloads all singleton beans upon context startup".

1:What is the difference between bean "loading" and bean "initialisation" or "creation"?
2:If we want to access a bean using the ApplicationContext,then do we need to use following code:



or the beans are automatically created without the above line.
Similarly ,is it necessary to give the following code in case of BeanFactory:
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

I recommend using the latest version of Spring in Action. You are reading an older book with older ways of doing things.

In today's Spring you do not need to know anything about the old BeanFactory. So forget about that sentence.

So, all you need to know is that Spring instantiates your objects/beans when the ApplicationContext is created. In that one line of code ApplicationContext context = new ClassPathXmlApplicationContext()

So in that one line of code, Spring is reading your configuration and then after it finishes reading all the xml, then it will instantiate all the beans in the xml and put it into a Map.

That is the default.

However, you can configure a bean to be a different scope or lazy. Which means that that one line does not instantiate the bean for you, but waits till later when you ask for it.

I personally have never set a bean to lazy because there hasn't been a use case I have come across that would require/need that.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Lazy and preloading of beans