🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this tutorial, we will learn how to use @Lazy annotation in Spring or Spring boot applications.
YouTube Video
@Lazy Annotation Overview
@Lazy Annotation Example
EagerLoader
import org.springframework.stereotype.Component;
@Component
public class EagerLoader {
public EagerLoader(){
System.out.println("EagerLoader ..");
}
}LazyLoader
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
@Component
@Lazy
public class LazyLoader {
public LazyLoader(){
System.out.println("LazyLoader..");
}
}Testing
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringAnnotationsApplication {
public static void main(String[] args) {
var context = SpringApplication.run(SpringAnnotationsApplication.class, args);
// EagerLoader eagerLoader = context.getBean(EagerLoader.class);
LazyLoader lazyLoader = context.getBean(LazyLoader.class);
}
}Note that the EagerLoader class bean was created eagerly at the startup/bootstrapping of the application context but the LazyLoader class bean is created when it requested using the getBean() method:
LazyLoader lazyLoader = context.getBean(LazyLoader.class);This clearly demonstrates that @Lazy annotation is used to load the Spring beans lazily (on-demand).
Using @Value Annotation with @Configuration & @Bean Annotations
If you annotated @Lazy annotation with @Configuration annotation then all the beans within the configuration file are loaded lazily:
import net.javaguides.springannotations.lazy.EagerLoader;
import net.javaguides.springannotations.lazy.LazyLoader;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
@Configuration
@Lazy
public class AppConfig {
@Bean
public LazyLoader lazyLoader(){
return new LazyLoader();
}
@Bean
public EagerLoader eagerLoader(){
return new EagerLoader();
}
}If you annotated @Lazy annotation with @Bean annotation then that particular bean within the configuration file is loaded lazily:
@Bean
@Lazy
public LazyLoader lazyLoader(){
return new LazyLoader();
}Related Spring and Spring Boot Annotations
- Spring Boot @Bean Annotation Example
- Spring @Qualifier Annotation Example
- Spring @Autowired Annotation with Example
- Spring @Bean Annotation with Example
- Spring @Configuration Annotation with Example
- Spring @PropertySource Annotation with Example
- Spring @Import Annotation with Example
- Spring @ImportResource Annotation Example
- Spring - @Lazy Annotation Example
- Spring - @Primary Annotation Example
- Spring @PostConstruct and @PreDestroy Example
- Spring @Repository Annotation
- Spring @Service Annotation
- The Spring @Controller and @RestController Annotations
- Spring Boot @Component, @Controller, @Repository and @Service
- Spring @Scope annotation with Prototype Scope Example
- Spring @Scope annotation with Singleton Scope Example
- Spring Boot @PathVariable
- Spring Boot @ResponseBody
- Spring @RequestBody - Binding Method Parameters to Request Body
- Spring Boot @ResponseStatus Annotation
- Spring Boot - Creating Asynchronous Methods using @Async Annotation
- @SpringBootTest Spring Boot Example
- @SpringBootTest vs @WebMvcTest
- @DataJpaTest Spring Boot Example
- Spring @PostConstruct and @PreDestroy Example
- Spring @GetMapping, @PostMapping, @PutMapping, @DeleteMapping and @PatchMapping
- Spring Boot @EnableAutoConfiguration Annotation with Example
- Spring Boot @SpringBootApplication Annotation with Example
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment