• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Lazy and preloading of beans

Ranch Hand
Posts: 238
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
Posts: 17347
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


    Bookmark Topic Watch Topic
  • New Topic