π Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
π 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 (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
By default, the Spring IoC container creates and initializes all singleton beans at application startup. However, we can prevent this pre-initialization by using the @Lazy annotation.
The @Lazy annotation may be used in any class directly or indirectly annotated with @Component or in methods annotated with @Bean.
YouTube Video - Spring @Lazy Annotation
Spring @Lazy Annotation Example
Create a Simple Maven Project
Project Structure
The below diagram shows a project structure for your reference -Maven Dependency
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>6.1.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.1.8</version>
</dependency>
Create Spring Beans - FirstBean and SecondBean
FirstBean.javapackage net.javaguides.spring.lazy;
public class FirstBean {
public FirstBean() {
System.out.println("Inside FirstBean Constuctor");
}
public void test() {
System.out.println("Method of FirstBean Class");
}
}
package net.javaguides.spring.lazy;
public class SecondBean {
public SecondBean() {
System.out.println("Inside SecondBean Constuctor");
}
public void test() {
System.out.println("Method of SecondBean Class");
}
}
Java-Based Configuration - AppConfig.java
Declare the above beans in the Java-based configuration class.package net.javaguides.spring.lazy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
@Configuration
public class AppConfig {
@Lazy(value = true)
@Bean
public FirstBean firstBean() {
return new FirstBean();
}
@Bean
public SecondBean secondBean() {
return new SecondBean();
}
}
Running Spring Application - Application.java
Let's create a main class and run an application.package net.javaguides.spring.lazy;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Application {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
FirstBean firstBean = context.getBean(FirstBean.class);
firstBean.test();
context.close();
}
}
Inside SecondBean Constuctor
Inside FirstBean Constuctor
Method of FirstBean Class
The source code of this article is available on my GitHub repository https://github.com/RameshMF/spring-core-tutorial
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
Comments
Post a Comment
Leave Comment