| Author |
How to decide whether to go for Spring lazy initilization Or Not?
|
Monica. Shiralkar
Ranch Hand
Joined: Jul 07, 2012
Posts: 186
|
|
|
i want to know when to go for Spring lazy initilization and when not to for go for lazy initilization.How to decide when to go for it.
|
 |
Ashwin Sridhar
Ranch Hand
Joined: Jul 09, 2011
Posts: 272
|
|
|
When you have more beans on your configuration and the possibilites of using few classes are very remote. You could mark them for lazy-init in order to avoid them from instantiating at start-up. Instead they would be instantiated when these beans are called.
|
Ashwin Sridhar
SCJP | SCWCD | OCA
|
 |
Monica. Shiralkar
Ranch Hand
Joined: Jul 07, 2012
Posts: 186
|
|
|
Thanks. Does lazy initilizing imporve performance? improving performance does it mean taking less time for executiion?
|
 |
Ashwin Sridhar
Ranch Hand
Joined: Jul 09, 2011
Posts: 272
|
|
Yes it improves performance. Why to load beans which are going to be used very rarely or not going to be used at all during the flow your code. It takes comparitively less time for start up.
But if these lazy-init beans are called during the flow of your logic, it would be instantiated then, which means execution time would be fraction of seconds more.
|
 |
Bill Gorder
Bartender
Joined: Mar 07, 2010
Posts: 1282
|
|
Just be careful not to prematurely optimize. Most of the time it is desirable for beans to be loaded eagerly, because errors in the configuration or surrounding environment are discovered immediately. Also be aware that marking it lazy is not a guarantee that it will be lazily initialized. If the lazy-initialized bean is a dependency of a singleton bean that is not lazy-initialized it will be eagerly loaded regardless (because it is required by another bean that is not lazy-initialized).
Just like a like a lot of things just because you can does not mean you should. I would leave everything as eagerly loaded and refrain from making these types of optimizations until there is reason to.
|
[How To Ask Questions][Read before you PM me]
|
 |
Monica. Shiralkar
Ranch Hand
Joined: Jul 07, 2012
Posts: 186
|
|
Thank You.
a you said "it is desirable for beans to be loaded eagerly, because "errors in the configuration or surrounding environment are discovered immediately".
What does immediately mean here with respect to any errors? because whether it is early loading or lazy loading errors will be detected at RUNTIME only?
|
 |
Bill Gorder
Bartender
Joined: Mar 07, 2010
Posts: 1282
|
|
|
Correct. But if it is eagerly loaded your errors in this case will be evident upon deployment, where in the other case it might be awhile before functionality is triggered at runtime causing those beans to be loaded and making the errors apparent. Like I said it is a great feature to have when you need it, but I have rarely ever had the need to use it. Premature optimization often causes more problems than it fixes. Just keep that in mind when you are looking at features like this, make sure you test them very well. I could mark some beans as lazy initialize because they are rarely needed, then not test it thoroughly in all environments and when it is triggered 60 days later in a production environment it bombs out. For this reason even if it is used rarely I often times will still eagerly load it unless it is a very resource intensive process that is used very rarely, in that case something like this might make sense, but it should be carefully evaluated on a case by case basis and applied only as needed.
|
 |
 |
|
|
subject: How to decide whether to go for Spring lazy initilization Or Not?
|
|
|