Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Lazy and preloading of beans  RSS feed

Sudhanshu Mishra
Ranch Hand
Posts: 238
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17309
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!