Lazy Initialize Spring Bean XML Configuration

By default spring container eagerly creates and configures all singleton beans. In most cases this is desirable, because errors and exceptions are discovered at startup time rather than runtime. When this behavior is not desirable, you can lazy initialize spring beans by marking the bean definition as lazy-initialized. This will not create the bean immediately but create the instance when it is first requested.

Beans

We have two beans which we are loading in our spring container. The first Workday will be loaded eagerly by the container.

package com.memorynotfound.spring.core.lazy;

public class Workday {

    public Workday() {
        System.out.println("Workday initialized");
    }
}

The second bean Holiday will be lazy initialized.

package com.memorynotfound.spring.core.lazy;

public class Holiday {

    public Holiday() {
        System.out.println("Holiday initialized");
    }

}

Lazy Initialize Spring Bean

By default the spring container creates and configures all beans eagerly. By setting the lazy-init attribute of the <bean/> element, we mark the bean to be lazy initialized by the container. Which means that the bean will be created when it is requested.

<?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.memorynotfound.spring.core.lazy.Workday"/>
    <bean class="com.memorynotfound.spring.core.lazy.Holiday" lazy-init="true"/>

</beans>

When a lazy-initialized bean is a dependency of another bean that is not lazy-initialized, the spring container creates the lazy-initialized bean at startup, because it must satisfy the singleton’s dependencies.

Running The Application

Next we bootstrap the application. After the application is wired, we print Application context loaded to the console. This to show you that the first bean will be eagerly created, before the application context initialization method returns. Afterwards we retrieve an instance of both beans.

package com.memorynotfound.spring.core.lazy;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Run {

    public static void main(String... args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("app-config.xml");
        System.out.println("Application context loaded");
        Holiday holiday = context.getBean(Holiday.class);
        Workday workday = context.getBean(Workday.class);
    }
}

Console Output

As noted above, the Workday bean is loaded before the application context initialization method returns. Afterwards we retrieve an instance of the Holiday bean, which is lazy-initialized.

INFO: Loading XML bean definitions from class path resource [app-config.xml]
Workday initialized
Application context loaded
Holiday initialized

References

Download

You may also like...