6

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 ?

1
  • 2
    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. Commented Apr 12, 2013 at 15:51

5 Answers 5

2

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

0

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

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

Comments

0

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.

1 Comment

Well, its something very similar to this: batmat.net/blog/post/2008/01/13/…, but I did also for annotated beans and beans imported from the first xml
0

Starting with Spring Boot 2 you can use the spring.main.lazy-initialization property to configure lazy initialization across the whole application.

Setting the property value to true means that all the beans in the application will use lazy initialization.

in application.yaml

spring:
  main:
    lazy-initialization: true

or in application.properties

spring.main.lazy-initialization=true

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.