spring-framework / org.springframework.context.annotation / Lazy

Lazy

@Target([AnnotationTarget.CLASS, AnnotationTarget.FILE, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER, AnnotationTarget.CONSTRUCTOR, AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.FIELD]) class Lazy

Indicates whether a bean is to be lazily initialized.

May be used on any class directly or indirectly annotated with or on methods annotated with Bean.

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

If Lazy is present on a Configuration class, this indicates that all @Bean methods within that @Configuration should be lazily initialized. If @Lazy is present and false on a @Bean method within a @Lazy-annotated @Configuration class, this indicates overriding the 'default lazy' behavior and that the bean should be eagerly initialized.

In addition to its role for component initialization, this annotation may also be placed on injection points marked with org.springframework.beans.factory.annotation.Autowired or javax.inject.Inject: In that context, it leads to the creation of a lazy-resolution proxy for all affected dependencies, as an alternative to using org.springframework.beans.factory.ObjectFactory or javax.inject.Provider.

Author
Chris Beams

Author
Juergen Hoeller

Since
3.0

See Also
PrimaryBeanConfigurationorg.springframework.stereotype.Component

Constructors

<init>

Lazy(value: Boolean)

Indicates whether a bean is to be lazily initialized.

May be used on any class directly or indirectly annotated with or on methods annotated with Bean.

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

If Lazy is present on a Configuration class, this indicates that all @Bean methods within that @Configuration should be lazily initialized. If @Lazy is present and false on a @Bean method within a @Lazy-annotated @Configuration class, this indicates overriding the 'default lazy' behavior and that the bean should be eagerly initialized.

In addition to its role for component initialization, this annotation may also be placed on injection points marked with org.springframework.beans.factory.annotation.Autowired or javax.inject.Inject: In that context, it leads to the creation of a lazy-resolution proxy for all affected dependencies, as an alternative to using org.springframework.beans.factory.ObjectFactory or javax.inject.Provider.

Properties

value

val value: Boolean

Whether lazy initialization should occur.