Home » Lazy initialization in Spring Boot

Lazy initialization in Spring Boot

  • by
Lazy initialization in Spring Boot

1. Overview

In this article, we will learn the lazy initialization concept in Spring boot. To learn more about Spring, refer to our articles.

2. Lazy initialization

By default, the Spring IOC container creates all the beans during the application start-up. However, you can configure the beans to be created lazily only when required.

SpringApplication allows an application to be initialized lazily. Once you enable the lazy initialization, the Spring container creates the beans only when required.

As a result, enabling lazy initialization can reduce the time that it takes your application to start.

In a web application, enabling lazy initialization will cause many web-related beans not to be initialized until the app receives an HTTP request.

2.1. Enable Lazy initialization in Spring boot

You can enable lazy initialization by using any of the below ways. Note that the spring.main.lazy-initialization property defined in the property files takes precedence over flags set programmatically using either SpringApplication or SpringApplicationBuilder.

The below configuration influences all the beans in the application context. If you want to configure lazy initialization for a specific bean, you can do using the @Lazy approach.

2.1.1. Property

You can enable lazy initialization by using the spring.main.lazy-initialization property:

// application.properties
spring.main.lazy-initialization=true
// application.yml
spring:
  main:
    lazy-initialization: true

2.1.2. lazyInitialization method

You can enable Lazy initialization programmatically using the lazyInitialization method on SpringApplicationBuilder.

package com.tedblob.profiles;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
@SpringBootApplication
public class Application {
	public static void main(String[] args) {
		new SpringApplicationBuilder(Application.class)
				.lazyInitialization(true)
				.build(args)
				.run();
	}
}

2.1.3. setLazyInitialization method

You can use the setLazyInitialization method on SpringApplication to enable:

@SpringBootApplication
public class Application {
	public static void main(String[] args) {
		SpringApplication app = new SpringApplication(Application.class);
		app.setLazyInitialization(true);
		app.run(args);
	}
}

3. Pros and Cons of Lazy initialization

3.1. Pros

  1. Improve the application startup time as lazy initialization decreases the number of beans created during the start-up

3.2. Cons

  1. Delays the discovery of the problem. For example, when a misconfigured bean is initialized lazily, a failure will no longer occur during startup and the problem will only become apparent when the bean is initialized.
  2. Must ensure that the JVM has sufficient memory available to accommodate all the application’s beans created later at run time. Recommended fine-tuning the JVM’s heap size before enabling lazy initialization.
  3. In a web application, bean creation on the first HTTP request can increase the latency and response time.

4. Conclusion

To sum up, we have learned to enable lazy initialization in a Spring boot application. You can find the code samples in our GitHub repository.