Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring default-lazy-init does not seems to work, i see Pre-instantiating of singletons

I am defining default-lazy-init="true" in the spring context file inside beans tag but when I start tomcat, I see my beans are getting instantiated. here is what it shows in log -

org.springframework.beans.factory.support.DefaultListableBeanFactory (DefaultListableBeanFactory.java:555) - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@ac6fb1: defining beans [dataSource,my other beans in app.......

Am I missing something ?

like image 978
Amit Avatar asked Oct 16 '25 23:10

Amit


1 Answers

Even if a bean is declared to be lazy initialized, it will still be initialized if another bean depends on it.

I'm going to assume from your log, that the bean in question is dataSource. I will also assume you have other beans that depend on dataSource (otherwise it wouldn't be very useful). If the context initializes the other beans and finds that, for example, it needs to autowire the dataSource bean, it will have to first initialize it.

If you want full lazy initialization, you will have to make every bean in some object graph be lazily initialized.

like image 147
Sotirios Delimanolis Avatar answered Oct 19 '25 12:10

Sotirios Delimanolis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!