In Spring Framework, the @Lazy annotation can be used to configure the lazy initialization of the beans. By default, Spring initializes all singleton beans eagerly at the application startup. However, in certain scenarios where the bean initialization is resource intensive or not immediately required, lazy initialization improves the application startup time and resource utilization.
Key Terminologies:
- Lazy Initialization: It postpones the creation of the object until it is first requested. This is useful when the object creation is resource-intensive or when the object is not immediately required during the application startup.
- Singleton Beans: These can be the beans for which the single instance is created per the Spring container. By default, all the spring beans are singleton beans unless configured otherwise.
Steps to Implement the @Lazy Annotation
- Step 1: Add @Lazy Annotation: We can add the @Lazy annotation to the bean definition to configure the lazy initialization of the project.
- Step 2: Configure the Application Context: We can ensure that the lazy initialization is enabled in the Spring application context of the configuration.
- Step 3: Use Lazy-initialized Beans: We can inject the lazy-initialized beans into the other components where needed of the Spring project.
Use of @Lazy annotation in a Spring Project
We will develop a simple Spring project with @Lazy Annotations.
Step 1: Create a Spring project using Spring Initializr. After creating successfully, add the necessary dependencies to the project as mentioned below.
Dependencies:
- Spring Web
- Lombok
- Spring Dev Tools
After creation is done, the file structure will look like the below image:

Step 2: Create the new Java package and it named as bean. In that package, create a new Java class named as TimeConsumingBean.
Go to src > main > java > lazydemo > bean > TimeConsumingBean and put the below code.
Java
package org.example.lazydemo.bean;
import jakarta.annotation.PostConstruct;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
@Component
@Lazy
public class TimeConsumingBean {
@PostConstruct
public void init() {
// Simulate time-consuming initialization
try {
Thread.sleep(5000); // Sleep for 5 seconds
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Time-consuming bean initialized!");
}
public void doSomething() {
System.out.println("Doing something with time-consuming bean...");
}
}
Step 3: Create the new Java package and it named as controller. In that package, create a new Java class named as MyController.
Go to src > main > java > lazydemo > controller > MyController and put the below code.
Java
package org.example.lazydemo.controller;
import org.example.lazydemo.bean.TimeConsumingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
@Autowired
private TimeConsumingBean timeConsumingBean; // Autowire the TimeConsumingBean
@GetMapping("/action")
public String performAction() {
timeConsumingBean.doSomething(); // Call the doSomething() method of TimeConsumingBean
return "Action performed!";
}
}
Step 4: Open the main class of the project and put the below code.
Java
package org.example.lazydemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class LazyDemoApplication {
public static void main(String[] args) {
SpringApplication.run(LazyDemoApplication.class, args);
}
}
build.gradle:
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.4'
id 'io.spring.dependency-management' version '1.1.4'
}
group = 'org.example'
version = '0.0.1-SNAPSHOT'
java {
sourceCompatibility = '17'
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
}
Step 5: Once the project completed, run it as spring application and it will run on port 8080. Refer the below image for better understanding.
Output:

Postman Output:
GET http://localhost:8080/action

Console Output:

- In the above project, we demonstrate the usage of the @Lazy annotation in the Spring Boot for lazy initialization of the time-consuming bean.
- The TimeConsumingBean simulates the time-consuming initialization processes and marks it with the @Lazy.
- We can refer its initialization until it's the first requested.
- It can help improve the application startup time.
- Especially, if the bean initialization process is the resource intensive and not the immediately required.
Similar Reads
Spring @Primary Annotation
@Primary annotation in Spring is used to indicate the primary bean when multiple beans of the same type are present for auto wiring. When multiple beans are eligible for auto wiring the @Primary annotation will help to determine which bean should be given preference. By applying @Primary annotation
4 min read
Spring Core Annotations
Spring Annotations are a form of metadata that provides data about a program. Annotations are used to provide supplemental information about a program. It does not have a direct effect on the operation of the code they annotate. It does not change the action of the compiled program.Spring Framework
5 min read
Spring Boot - Annotations
Spring Boot Annotations are a form of metadata that provides data about a spring application. Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the
7 min read
Spring Boot - @Async Annotation
Synchronization means every process or thread should be Synchronized together. It means executing process by process that's we can say Synchronization. This is used when one task is complete and only then does another task want to come and be executed. so It looks like there are some limitations. Th
2 min read
Spring @Autowired Annotation
The @Autowired annotation in Spring marks a constructor, setter method, property, or configuration method to be autowired. This means that Spring will automatically inject the required dependencies (beans) at runtime using its Dependency Injection mechanism. The image below illustrates this concept:
3 min read
Spring Security Annotations
There are multiple annotations supported by Spring Security. But, in this article, we will discuss about these annotations can be used in a Spring Boot project as well. These annotations play a crucial role in creating a web application in Spring Boot. The Spring Security annotations are a powerful
3 min read
Spring Framework Annotations
Spring framework is one of the most popular Java EE frameworks. It is an open-source lightweight framework that allows Java EE 7 developers to build simple, reliable, and scalable enterprise applications. Spring framework mainly focuses on providing various ways to help you manage your business obje
6 min read
Spring - Required Annotation
Consider a scenario where a developer wants to make some of the fields as mandatory fields. using the Spring framework, a developer can use the @Required annotation to those fields by pushing the responsibility for such checking onto the container. So container must check whether those fields are be
7 min read
Spring Data JPA - @Id Annotation
Spring Data JPA is a important part of Spring Boot applications, providing an abstraction over JPA (Java Persistence API) and simplifying database interactions. JPA is a specification that defines a standard way to interact with relational databases in Java, while Hibernate is one of the most widely
3 min read
Spring AOP - AspectJ Annotation
AOP i.e Aspect-Oriented Programming complements OOP by enabling modularity of cross-cutting concerns. @AspectJ is mainly used for declaring aspects as regular Java classes annotated with annotations. Spring AspectJ AOP implementation has many annotations. They are explained below: @Aspect: It declar
4 min read