Search This Blog

Spring - @Lazy Annotation Example

In this quick article, we’ll discuss Spring’s @Lazy annotation with an example.

By default, the Spring IoC container creates and initializes all singleton beans at time of application startup. We can prevent this pre-initialization of a singleton bean by using the @Lazy annotation.

The @Lazy annotation may be used on any class directly or indirectly annotated with @Component or on methods annotated with @Bean. In this example, we will use a Java-based configuration(using @Configuration and @Bean).

Spring @Lazy Annotation Example

Let's create an example to demonstrates usage of use @Lazy annotation in a spring application.

Tools and technologies used

Spring Framework - 5.1.0.RELEASE

JDK - 8 or later

Maven - 3.2+

IDE - Eclipse Mars/STS

Create a Simple Maven Project

Create a simple maven project using your favorite IDE and refer below section for packaging structure. If you are new to maven then read this article How to Create a Simple Maven Project.