You are currently viewing Spring Boot @Lazy Annotation

Spring Boot @Lazy Annotation

In Spring Boot, the @Lazy annotation is used to delay the initialization of a bean until the bean is actually needed. This can be useful when you have beans that are expensive to create or when you want to control when a bean is instantiated. In this tutorial, we’ll explore how to use @Lazy with examples.

1. Adding @Lazy Annotation

Let’s say we have a simple Spring Boot application with a couple of beans. We want to lazy-initialize one of these beans using the @Lazy annotation.

1.1. Create a Spring Boot Project

If you haven’t already set up a Spring Boot project, you can do so by using Spring Initializr or any preferred method. Make sure you include the necessary dependencies, such as spring-boot-starter-web, to create a basic Spring Boot application.

1.2. Create Beans

For demonstration, let’s create two beans: one lazy and one eager.

Eager Bean
package com.example.lazydemo.beans;

import org.springframework.stereotype.Component;

@Component
public class EagerBean {
    public EagerBean() {
        System.out.println("EagerBean Initialized!");
    }

    public void doSomething() {
        System.out.println("EagerBean doing something...");
    }
}
Lazy Bean
package com.example.lazydemo.beans;

import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

@Component
@Lazy
public class LazyBean {
    public LazyBean() {
        System.out.println("LazyBean Initialized!");
    }

    public void doSomething() {
        System.out.println("LazyBean doing something...");
    }
}

1.3. Create a Service

Next, let’s create a simple service that autowires both beans and uses them.

package com.example.lazydemo.services;

import com.example.lazydemo.beans.EagerBean;
import com.example.lazydemo.beans.LazyBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MyService {
    private final EagerBean eagerBean;
    private final LazyBean lazyBean;

    @Autowired
    public MyService(EagerBean eagerBean, LazyBean lazyBean) {
        this.eagerBean = eagerBean;
        this.lazyBean = lazyBean;
    }

    public void doSomething() {
        eagerBean.doSomething();
        lazyBean.doSomething();
    }
}

1.4. Main Application

Finally, create a main class to run the application:

package com.example.lazydemo;

import com.example.lazydemo.services.MyService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class LazyDemoApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(LazyDemoApplication.class, args);
        MyService myService = context.getBean(MyService.class);
        myService.doSomething();
    }
}

2. Running the Application

When you run the application, you will see the following output:

EagerBean Initialized!
LazyBean Initialized!
EagerBean doing something...
LazyBean doing something...

Explanation

  • EagerBean is eagerly initialized because it is created along with the context.
  • LazyBean, on the other hand, is lazily initialized. This means it won’t be created until it is needed. In this case, it’s needed when MyService is constructed.

When to Use @Lazy

You might consider using @Lazy in the following scenarios:

  • Expensive Resource Initialization: If creating the bean is time-consuming or resource-intensive, you can delay its creation until necessary.
  • Circular Dependencies: If you have circular dependencies between beans, @Lazy can help resolve initialization issues.
  • Startup Optimization: For beans that are not always needed during application startup, delaying their creation can improve startup time.

Conclusion

In this tutorial, you’ve learned how to use the @Lazy annotation in a Spring Boot application. By marking a bean as @Lazy, you can control when the bean is initialized, allowing for more efficient resource management and better performance in certain scenarios.

Leave a Reply