Learnitweb

Lazy Initialization in Spring Boot

1. Introduction

In this tutorial, we’ll discuss lazy initialization in Spring Boot application. By default in Spring, beans are created at application startup. However, we can override this default behavior. There may be a requirement to not create a bean at application startup. The reason could be many. If the creation of bean is too expensive and the bean is not required frequently, you can lazily create the bean. Beans which represent a functionality of application which is rarely used are good candidate of lazy creation.

When we configure a bean with lazy initialization, the bean will be created only when it is needed and its dependencies injected when needed. A bean configured as to be lazily initialized will not be created at application startup.

Since bean is lazily created and initialized, you have to make sure that the JVM has sufficient memory to allow creation of beans when there is a request. Sometimes it may not be possible to actually assess the memory required. So it is recommended to fine-tune the JVM heap size when you enable lazy initialization in an application.

2. Benefit of lazy initialization

By default lazy initialization is disabled. When lazy initialization of application is enabled, beans are created when needed and not at application startup. Since less number of beans are created at application startup, the boot time of application is reduced and application starts faster. The main benefit of lazy initialization of application is improvement in application startup time.

3. Downside of lazy initialization

The downside of lazy initialization of application is that if there is any problem with configuration of the bean, it could not be found at application startup. It can only be found when bean is actually created when application receives a request for the bean. This could be a big problem if the application is in production environment. It is always desirable to know the problems in the application as soon as possible.

4. Enable Lazy Initialization

We’ll discuss three ways to enable lazy initialization in application.

4.1 Using spring.main.lazy-initialization property

Lazy initialization can be enabled setting the property spring.main.lazy-initialization to true in application.properties.

spring.main.lazy-initialization=true

We can also define the property in application.yml:

spring:
  main:
    lazy-initialization: true

This configuration affects all beans in the context. If you want to disable lazy initialization for certain beans of the application, you can explicitly set the lazy attribute to ‘false’: @Lazy(false)

4.2 Using lazyInitialization method

You can set lazy initialization for the property by using lazyInitialization method on SpringApplicationBuilder.

new SpringApplicationBuilder(MyApplication.class).lazyInitialization(true).build().run(args);

4.3 Using setLazyInitialization method

You can use setLazyInitialization method on SpringApplication:

SpringApplication app = new SpringApplication(MyApplication.class);
app.setLazyInitialization(true);
app.run(args);

Property defined in the application property files take precedence over flags set using either SpringApplication or SpringApplicationBuilder.

5. Example of Lazy Initialization in Spring Boot

In this example, we’ll see lazy initialization with the help of a simple REST controller. We’ll create a controller /hello which return a string hello. We’ll observe that when lazy initialization is enabled, instance of HelloController will be created only when we create a request for the controller and not at application startup.

For this example, there is no specific dependency. We just need spring-boot-starter-web dependency to be declared in pom.xml.

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>

Following is the code of controller.

package com.learnitweb.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
	@RequestMapping("/")
	public String hello() {
		return "Hello";
	}
}

To enable lazy initialization, set spring.main.lazy-initialization to true in application.properties.

spring.main.lazy-initialization=true
logging.level.org.springframework=DEBUG

We have set logging level to DEBUG to view bean creation messages in console.

When you start the application, you’ll not see any message related to creation of HelloController instance in the console.

Now, try to access localhost:8080/hello and observe messages in console. You’ll see message like the following in console:

[nio-8080-exec-1] o.s.b.f.s.DefaultListableBeanFactory     : Creating shared instance of singleton bean 'helloController'

6. Conclusion

In this tutorial, we discussed lazy initialization in Spring Boot application. This is an important concept to learn from interview point of view. By default, lazy initialization is disabled. You should different ways to enable it.

Happy Learning !!!