Question

Does anyone know how to specify a bean as non lazy when using annotations to configure the bean?

Was it helpful?

Solution

Beans are not lazy by default. However as far as annotations are concerned it seems like currently annotations do not support it. http://forum.springsource.org/showthread.php?t=62931

Spring's next version though seem to have something in store http://jira.springframework.org/browse/SJC-263

OTHER TIPS

In spring 3.0 there is an annotation: @Lazy(false). But note that beans are eager by default.

Just to set things straight, be known that as to Spring 3.0 and later, beans are by default eagerly initialized.

Excerpt from the @Lazy(false) link in Bozho's answer:

If this annotation is not present on a Component or Bean definition, eager initialization will occur. If present and set to true, the Bean/Component will not be initialized until referenced by another bean or explicitly retrieved from the enclosing BeanFactory. If present and set to false, the bean will be instantiated on startup by bean factories that perform eager initialization of singletons.

I tried @EnableScheduling in my Configuration Annotation class and that did the trick.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top