1. Overview

  • When we enable lazy initialization then the bean will be initialization at the time on demand, Spring by default initialization beans at the start of the application.
  • For the web application, the Controller bean will be initialed on the first HTTP requires on that controller.
  • @Lazy(false) annotation using that we can disable Lazy for specific bean
  • NOTE: If something is misconfigured at Bean initialization then it can not be identified at the time of application startup, in that case, the application will start successful but error occur when initialization will happen just because of this reason spring lazy init false by default.

For example:

DemoController.Java

package com.javadeveloperzone;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping
public class DemoController {

    DemoController(){
        System.out.println("Controller init...");
    }
    
    @Autowired
    private EmployeeService employeeService;

    @RequestMapping("/demo")
    public String demo(){
        System.out.println("Preparing HTTP request...");
        return "This is from demo";
    }
}

EmployeeService.Java

package com.javadeveloperzone;

import org.springframework.stereotype.Service;

@Service
public class EmployeeService {

    public EmployeeService(){
        System.out.println("Service Init ...");
    }

}

Output:

On the first request, dependent beans will be initialized.

wget http://localhost:8181/demo

Controller init...
Service Init ...
Preparing HTTP request...

2. Enable Lazy Initialization

Way 1: Enable lazy using application.properties

true value indicates lazy initialization

spring.main.lazy-initialization=true

Way 2: Enable lazy using SpringApplication

true value indicates lazy initialization

@SpringBootApplication
public class SpringBootConfig {
    public static void main(String[] args) throws Exception {
        SpringApplication application = new SpringApplication(SpringBootConfig.class);
        application.setLazyInitialization(true);
        application.run();
    }
}

3. Disable lazy for specific bean

When we enable lazy initialization at that time all the beans will be lazily initialized. but we can also disable lazy initialization for specific beans using @Lazy(value = false) at bean level.

@Service
@Lazy(value = false)                // disable lazy for this bean
public class EmployeeService {

    public EmployeeService(){
        System.out.println("Service Init ...");
    }

}

3. Conclusion

In this article, we learned about Spring boot enable Lazy Initialization of bean using application.properties and SpringApplication with examples. We also learned about how we can disable Lazy initialization for the specific beans.

4. References

Was this post helpful?

Leave a Reply

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