Spring Boot @Lazy Example

1. Introduction

In Spring Boot, the @Lazy annotation is used when you want a bean to be initialized only when it is first used, not at startup. This can be particularly useful when you have a bean that is resource-intensive and you want to delay its creation to avoid impacting the startup time of the application.

Key Points:

1. @Lazy ensures that the bean is created and initialized only when it is first requested for.

2. It can be applied at the bean level or at the injection point.

3. Using @Lazy can improve the startup time of a Spring Boot application by avoiding the initialization of beans that may not be needed immediately.

2. Implementation Steps

1. Annotate a bean with @Lazy to indicate lazy initialization.

2. Inject this lazy bean into a component.

3. Access the bean's methods from the component to trigger its initialization.

4. Observe the behavior of the application at startup and when the bean is accessed.

3. Implementation Example

Here is the complete code that demonstrates the usage of @Lazy annotation:
// Step 1: Define a bean with @Lazy annotation
@Configuration
public class AppConfig {

    @Bean
    @Lazy
    public ExpensiveService expensiveService() {
        return new ExpensiveService();
    }
}

// Step 1: Create the service class to be lazily initialized
class ExpensiveService {
    public ExpensiveService() {
        System.out.println("ExpensiveService is being created");
    }

    public String perform() {
        return "Expensive Service is now running";
    }
}

// Step 2: Inject the lazy bean into a component
@Component
public class SimpleService {

    private final ExpensiveService expensiveService;

    @Autowired
    public SimpleService(ExpensiveService expensiveService) {
        this.expensiveService = expensiveService;
    }

    public void executeExpensiveService() {
        System.out.println(expensiveService.perform());
    }
}

// Step 3: Create the main application class
@SpringBootApplication
public class LazyAnnotationExampleApplication implements CommandLineRunner {

    @Autowired
    private SimpleService simpleService;

    public static void main(String[] args) {
        SpringApplication.run(LazyAnnotationExampleApplication.class, args);
        System.out.println("Spring Boot application started");
    }

    @Override
    public void run(String... args) {
        // Step 4: Access the bean's method to trigger initialization
        simpleService.executeExpensiveService();
    }
}

Output:

Spring Boot application started
ExpensiveService is being created
Expensive Service is now running

Explanation:

1. @Configuration class AppConfig defines a bean method expensiveService with the @Lazy annotation, indicating that the bean should not be created until it is first needed.

2. The ExpensiveService class prints a message in its constructor to indicate when it is being initialized.

3. SimpleService is a component that depends on the ExpensiveService. It will not trigger the creation of ExpensiveService until one of its methods is called.

4. LazyAnnotationExampleApplication is marked with @SpringBootApplication and implements CommandLineRunner to execute a method after the application has started.

5. Upon application startup, there is no output from ExpensiveService, demonstrating it has not been initialized yet.

6. When executeExpensiveService is called within the run method, it triggers the creation of ExpensiveService, resulting in the output showing that ExpensiveService is being created, followed by "Expensive Service is now running".


Comments