Michael Wiles
Michael Wiles

Reputation: 21186

how to specify a bean as non lazy with annotations

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

Upvotes: 34

Views: 35389

Answers (4)

dewrich
dewrich

Reputation: 13

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

Upvotes: -3

Bozho
Bozho

Reputation: 597114

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

Upvotes: 36

yair
yair

Reputation: 9245

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.

Upvotes: 9

Priyank
Priyank

Reputation: 14387

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

Upvotes: 10

Related Questions