Spring Boot Lazy Loading Beans Example - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, we are going to see how to enable Spring Boot Lazy Loading of beans while running the application.
Spring Framework has support for lazy bean initialisation since along ago. In spring by default; if the application context being refreshed, every bean in the context will be created, and its dependencies will be injected.
If we enable Spring Boot Lazy Loading, the beans will not be created and it won’t be injected the dependencies while refreshing the application context.
Based on your spring boot version number, you can enable Lazy Loading in different ways.
Spring Boot 2.2
Spring Boot 2.2 introduced a new property called spring.main.lazy-initialization. It makes the developer life more comfortable. We can directly add this property to your application.properties or an application.yml file like the following.
spring.main.lazy-initialization=true

spring:
  main:
    lazy-initialization: true

If you configure the above feature in your application.properties/yml file; all your beans will be loaded and injected on demand. If you wanted to make some specific beans to Lazy, you have to go with legacy approach like the following.
It is precisely similar to our old approach like the following.
If you use the @Lazy annotation on top of the @Configuration class, it says that all the methods with @Bean annotation should be loaded lazily.
@Lazy
@Configuration
@ComponentScan(basePackages = "com.onlinetutorialspoint.lazy")
public class AppConfigs {

    @Bean
    public Item getItem(){
        return new Item();
    }

    @Bean
    public Category getCategory(){
        return new Category();
    }
}
If you wanted to make some specific beans as lazy; you should remove the @Lazy annotation from AppConfig class and declare it on top of selected bean methods like the following.
@Bean
@Lazy(true)
public Item getItem(){
    return new Item();
}
We can autowire a Lazy bean like the below
Create a Lazy bean first:
@Bean
@Lazy
public Category getCategory(){
    return new Category();
}
And refer the category bean like below.
public class Item {
    @Lazy
    @Autowired
    Category category;
}
Here the @Lazy annotation is mandatory in both the places.
Spring Boot 2.2 Lazy Initialization
Spring Boot reading application properties
Happy Learning 🙂

Spring Boot H2 Database + JDBC Template Example
Spring Boot Hazelcast Cache Example
Spring Boot EhCache Example
Spring Boot RabbitMQ Message Publishing Example
Spring Boot Redis Cache Example – Redis Server
Spring Boot In Memory Basic Authentication Security
Sending Spring Boot Kafka JSON Message to Kafka Topic
Spring Boot JNDI Configuration – External Tomcat
Spring Boot Kafka Consume JSON Messages Example
Spring Boot DataRest Example RepositoryRestResource
How to Get All Spring Beans Details Loaded in ICO
Spring Boot MongoDB + Spring Data Example
Spring Boot Hibernate Integration Example
Spring Boot FileUpload Ajax Example
How to change Spring Boot Tomcat Port Number

Spring Boot H2 Database + JDBC Template Example
Spring Boot Hazelcast Cache Example
Spring Boot EhCache Example
Spring Boot RabbitMQ Message Publishing Example
Spring Boot Redis Cache Example – Redis Server
Spring Boot In Memory Basic Authentication Security
Sending Spring Boot Kafka JSON Message to Kafka Topic
Spring Boot JNDI Configuration – External Tomcat
Spring Boot Kafka Consume JSON Messages Example
Spring Boot DataRest Example RepositoryRestResource
How to Get All Spring Beans Details Loaded in ICO
Spring Boot MongoDB + Spring Data Example
Spring Boot Hibernate Integration Example
Spring Boot FileUpload Ajax Example
How to change Spring Boot Tomcat Port Number
 


Δ
 Spring Boot – Hello World
 Spring Boot – MVC Example
 Spring Boot- Change Context Path
 Spring Boot – Change Tomcat Port Number
 Spring Boot – Change Tomcat to Jetty Server
 Spring Boot – Tomcat session timeout
 Spring Boot – Enable Random Port
 Spring Boot – Properties File
 Spring Boot – Beans Lazy Loading
 Spring Boot – Set Favicon image
 Spring Boot – Set Custom Banner
 Spring Boot – Set Application TimeZone
 Spring Boot – Send Mail
 Spring Boot – FileUpload Ajax
 Spring Boot – Actuator
 Spring Boot – Actuator Database Health Check
 Spring Boot – Swagger
 Spring Boot – Enable CORS
 Spring Boot – External Apache ActiveMQ Setup
 Spring Boot – Inmemory Apache ActiveMq
 Spring Boot – Scheduler Job
 Spring Boot – Exception Handling
 Spring Boot – Hibernate CRUD
 Spring Boot – JPA Integration CRUD
 Spring Boot – JPA DataRest CRUD
 Spring Boot – JdbcTemplate CRUD
 Spring Boot – Multiple Data Sources Config
 Spring Boot – JNDI Configuration
 Spring Boot – H2 Database CRUD
 Spring Boot – MongoDB CRUD
 Spring Boot – Redis Data CRUD
 Spring Boot – MVC Login Form Validation
 Spring Boot – Custom Error Pages
 Spring Boot – iText PDF
 Spring Boot – Enable SSL (HTTPs)
 Spring Boot – Basic Authentication
 Spring Boot – In Memory Basic Authentication
 Spring Boot – Security MySQL Database Integration
 Spring Boot – Redis Cache – Redis Server
 Spring Boot – Hazelcast Cache
 Spring Boot – EhCache
 Spring Boot – Kafka Producer
 Spring Boot – Kafka Consumer
 Spring Boot – Kafka JSON Message to Kafka Topic
 Spring Boot – RabbitMQ Publisher
 Spring Boot – RabbitMQ Consumer
 Spring Boot – SOAP Consumer
 Spring Boot – Soap WebServices
 Spring Boot – Batch Csv to Database
 Spring Boot – Eureka Server
 Spring Boot – MockMvc JUnit
 Spring Boot – Docker Deployment