The Spring bean Lazy loading xml configuration example show you how can lazy initialize spring beans. Spring container eagerly creates and configures all singleton beans by default. If your bean configuration is wrong Spring container throws errors and exceptions at startup time. However, you can lazy initialize spring beans at runtime by marking the bean definition as lazy-initialized. This will not create the bean immediately but create the instance when it is first requested.
Create Spring Beans
In this Spring bean Lazy loading xml configuration example, we create two beans Student and Book.
Student bean
1 2 3 4 5 6 7 8 |
package com.javabycode.spring.core.lazy; public class Student { public Student() { System.out.println("Student initialized"); } } |
Book bean
1 2 3 4 5 6 7 8 |
package com.javabycode.spring.core.lazy; public class Book { public Book() { System.out.println("Book initialized"); } } |
Lazy Initialize Spring Bean
Our two beans will be loading in spring container. The bean Student will be loaded eagerly by default. The bean Book will be lazy initialized by setting the lazy-init attribute of the
1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean class="com.javabycode.spring.core.lazy.Student"/> <bean class="com.javabycode.spring.core.lazy.Book" lazy-init="true"/> </beans> |
By setting the attribute lazy-init=”true”, the container will lazy initialized this bean instead of loading eagerly. It means that the bean will be created when it is first requested.
Demo Application
In this Spring bean Lazy loading xml configuration example, We create the main class to bootstrap the application. We print the message after context loaded and retrieve an instance of Book bean. However, both of Student and Book beans are loaded, let’s see the below:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.javabycode.spring.core.lazy; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MySpringApplication { public static void main(String... args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); System.out.println("Application context loaded"); Book book = context.getBean(Book.class); } } |
Running the above main and see the output. You will see the bean Student is loaded eagerly and the bean Book is lazy loaded.
1 2 3 4 |
INFO: Loading XML bean definitions from class path resource [spring-config.xml] Student initialized Application context loaded Book initialized |
One more thing, if a lazy-initialized bean is a dependency of another bean that is not lazy-initialized, the spring container must make sure the singleton’s dependencies so that it must creates the lazy-initialized bean at startup. Now we will discuss other example to classify this issues.
We add a Course bean that has a dependency which is Book bean like below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.javabycode.spring.core.lazy; public class Course { private Book book; public void setBook(Book book) { this.book = book; } public Course() { System.out.println("Book initialized"); } } |
then configure the dependency of Course bean in spring-config.xml
1 2 3 |
<bean id="course" class="com.javabycode.spring.core.lazy.Course" autowire="byType"> <property name="book" ref="book"/> </bean> |
Running the above main again and see the output. You will see all the beans is loaded eagerly via the below message:
1 2 3 4 5 |
INFO: Loading XML bean definitions from class path resource [spring-config.xml] Student initialized Book initialized Book initialized Application context loaded |
That’s all on the Spring bean Lazy loading xml configuration example.
References
Lazy init beans Spring Doc
Download complete source code, click link below
spring-bean-lazy-loading-xml-configuration-example.zip (285 downloads)