DEV Community

Willian Ferreira Moya
Willian Ferreira Moya

Posted on • Originally published at springmasteryhub.com

3 Ways to Use the @Lazy Annotation in Spring

Does your Spring application take too long to start? Maybe this annotation could help you.

This annotation indicates to Spring that a bean needs to be lazily initiated.

Spring will not create this bean at the start of the application. Instead, it will create it only when the bean is requested for the first time.

This allows Spring start-up to be faster. However, the first interaction with the bean can be slow because of the time required to create and inject the bean at runtime.

You can use this annotation directly in the bean class (beans annotated with @Component and other stereotypes). Also, it can be used in methods annotated with @bean.

It’s possible to annotate a @Configuration class, making all the beans from that class become lazily initiated.

Let’s see some examples of how to use it.

1. Using @lazy Directly in the Class

@Service
@Lazy
public class EmailService {

    public EmailService() {
        System.out.println("EmailService bean is created!");
    }

    public void sendEmail(String message) {
        System.out.println("Sending email with message: " + message);
    }
}
Enter fullscreen mode Exit fullscreen mode

2. Using @lazy in a Bean Method

@Configuration
public class AppConfig {

    @Bean
    @Lazy
    public EmailService emailService() {
        System.out.println("EmailService bean is being created!");
        return new EmailService();
    }
}
Enter fullscreen mode Exit fullscreen mode

3. Using @lazy with a @Configuration Class

@Lazy
@Configuration
public class AppConfig {

    @Bean
    public EmailService emailService() {
        System.out.println("EmailService bean is being created!");
        return new EmailService();
    }
}
Enter fullscreen mode Exit fullscreen mode

Use Case Scenarios for @lazy Annotation

This annotation can be useful when you are working with some beans that are resource-intensive to create.

You could use this annotation, so they don’t affect the startup of your application, making the application become available fast by not loading this bean at the start-up.

Remember this decision is a trade-off, your application may start faster, but it will slow down the resource that uses this resource-intensive bean, in the first usage. Spring will setup the bean that was not created at the application start-up.

Another scenario is to use this annotation in beans that are rarely used.

Let’s say your application has an EmailService that needs to send emails once a week.

You won’t need this right away, so it’s fine to set this bean with lazy initialization.

Conclusion

Now you understand how to use @lazy in your project. Can you find places on your application that you can apply it? Share your thoughts and let me know in the comments!

If you like this topic, make sure to follow me. In the following days, I’ll be explaining more about Spring annotations! Stay tuned!

Follow me!

Top comments (0)