
What is @Lazy Annotation? Purpose of @Lazy with example - Spring & Spring Boot Annotations Series #9
In this video, we are going to discuss about the @Lazy.
Spring Annotation
Spring Boot Annotation
learn to use @Lazy annotation in spring boot.
What is @Lazy Annotation? Purpose of @Lazy with example - Spring & Spring Boot Annotations Series #9
@Lazy annotation is used to indicate that a bean should be lazily initialized.
This can be useful in scenarios where you want to optimize resource usage by delaying the creation of a bean until it is required.
how @Lazy works:
Normal Initialization: By default, Spring initializes all beans eagerly when the application context is being created.
Lazy Initialization: When you annotate a bean with @Lazy, it tells Spring to defer the initialization of that bean until it is explicitly requested.
NOTE: Remember that lazy initialization is not suitable for all scenarios, and it depends on the specific use case whether it provides any performance benefits.
1. Deferred Initialization: By default, Spring initializes all beans eagerly during the application context startup. However, in some cases, creating certain beans might be resource-intensive or time-consuming. The @Lazy annotation allows you to defer the initialization of such beans until they are actually needed.
2. Improved Startup Performance: Lazy initialization can help improve the startup performance of your Spring Boot application. Beans that are not immediately required are not instantiated during the application's initialization, which can lead to faster startup times.
3. Resource Optimization: If a bean represents a resource-intensive operation or a component that may not be used in every application flow, lazy initialization can be a way to optimize resource usage and reduce memory overhead.
4. Circular Dependency Resolution: @Lazy can also be useful in resolving circular dependencies. If there are circular dependencies between beans, lazy initialization can help break the circular reference and allow the beans to be created when needed.
#spring #springboot #@lazy
コメント