Spring

Spring @Lazy Annotation

Introduction:

The Spring framework, by default, loads and eagerly initializes all beans at the application startup itself. In our application, we might have some pretty resource-intensive beans. We’ll prefer to load such beans on a need basis. We can achieve this using the Spring @Lazy annotation.

In this tutorial, we’ll learn how to use @Lazy annotation to lazily load our beans.

Lazy Initialization:

If we mark our Spring configuration class with @Lazy annotation, then all defined beans with @Bean annotation will get lazily loaded:

We can also lazy load just a single bean by using this annotation at the method level:

Testing Lazy Loading:

Let’s quickly test out this functionality by running our application:

On our console, we’ll see:

Clearly, Spring initialized both Student and Teacher beans on demand and not while setting up the application context.

With @Autowired Annotation:

We can also use @Lazy annotation at an injection point: constructor, setter or field-level. 

Let’s say we have a Classroom class that we want to lazy-load:

And it’s wired to the University bean via @Autowired annotation:

Here, we have lazily-injected the Classroom bean. And so, while instantiating a University object, Spring will create and map a proxy Classroom object to it. Finally, when we invoke useClassroomBean(), only then it’ll create the actual Classroom instance:

The above code will produce the following logs:

As we can see, the instantiation of the Classroom object gets delayed until its actually needed.

Please note that, for lazy-injection, we must use @Lazy annotation on both the component class as well as the injection point.

Conclusion:

In this quick tutorial, we learned how to lazy load our Spring beans. We talked about lazy initialization and lazy injection.

Be the First to comment.

Leave a Comment

Your email address will not be published. Required fields are marked *