5
votes

Is there a spring property to lazy-init all beans that spring framework loads ?

I know about these properties

 - lazy-init="true"
 - default-lazy-init="true"

however there are multiple spring config xml files and some are packaged within jar so dont have liberty to change neither <bean> nor <beans> tag.

Any other way to tackle this via configuration ? or programatically ?

4
I really wish I could do this. We are autowiring a lot of beans, so it takes 15-20 seconds to pre-instantiate them all. I would love to change this on my developer machine to improve startup time, but keep it the same on the production server. - theblang

4 Answers

2
votes

Short of extending the Spring bean loader, none that I know of.

2
votes

You caN also use @Lazy annotation, but it is the same as you mentioned above.

0
votes

According to java doc this should work ( though it looks not nice)

if (context.getBeanFactory() instanceof DefaultListableBeanFactory)
    {
        ((DefaultListableBeanFactory) context.getBeanFactory()).setAllowEagerClassLoading(false);
    }
0
votes

I've implemented this on my company, had to extend some classes of spring tough. It wasn't easy, but we gained about 20s on every tomcat startup. Unfortunately, for privacy clauses, I can't show the code, but take a look at ClassPathBeanDefinitionScanner,DefaultBeanDefinitionDocumentReader,ContextNamespaceHandler and ComponentScanBeanDefinitionParser classes.