I've got a slightly out-of-the-ordinary situation regarding lazy initialization...
I've got a specific singleton (root) bean that has a nested graph of dependencies on other inner beans. I want the 'leaf' inner beans to be lazy-initialized, since they acquire expensive resources, and not all of them are necessarily used in each application run.
However, it is obvious that with the standard container/bean lifecycle, this entire bean 'tree' is initialized in one go, so the lazy-init setting is only really meaningful at the root bean level.
It's a pity Spring doesn't provide some sort of lazy-init-placholder mechanism that caches the BeanDefinition and only instantiates the bean instance on demand. I'm actually considering doing this myself, with the use of a custom BeanDefinitionParser: the only problem I have is that none of the BeanFactory types expose a public method to create a bean instance from an already-created BeanDefinition. The only way to get hold of that kind of method is to subclass a BeanFactory implementation.
Anyone done something like this before?


Reply With Quote
.
roxy/> tag would help? I'm not 100% this will solve your problem but Spring will create a proxy for you and inject that into your parent bean, and then hopefully only when you invoke the proxy would your lazy singletons be instantiated. Maybe give it a try!
