spring

Spring Lazy Initialization

Lazy initialization in the configuration class

@Configuration
// @Lazy - For all Beans to load lazily
public class AppConf {

    @Bean
    @Lazy
    public Demo demo() {
        return new Demo();
    }
}

For component scanning and auto-wiring

@Component
@Lazy
public class Demo {
    ....
    ....
}

@Component
public class B {

    @Autowired
    @Lazy // If this is not here, Demo will still get eagerly instantiated to satisfy this request.
    private Demo demo;

    .......
 }

Example of Lazy Init in Spring

The @Lazy allow us to instruct the IOC container to delay the initialization of a bean. By default, beans are instantiated as soon as the IOC container is created, The @Lazy allow us to change this instantiation process.

lazy-init in spring is the attribute of bean tag. The values of lazy-init are true and false. If lazy-init is true, then that bean will be initialized when a request is made to bean. This bean will not be initialized when the spring container is initialized. If lazy-init is false then the bean will be initialized with the spring container initialization and this is the default behavior.

app-conf.xml

<beans xmlns="https://www.springframework.org/schema/beans" 
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns:util="https://www.springframework.org/schema/util"
xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-3.0.xsd
https://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util-3.0.xsd">
 
<bean id="testA" class="com.concretepage.A"/>
<bean id="testB" class="com.concretepage.B" lazy-init="true"/>

A.java

package com.concretepage;
public class A {
public A(){
    System.out.println("Bean A is initialized");
   }
}

B.java

package com.concretepage;
public class B {
public B(){
    System.out.println("Bean B is initialized");
   }
} 

SpringTest.java

package com.concretepage;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
public static void main(String[] args) {
     ApplicationContext context = new ClassPathXmlApplicationContext("app-conf.xml");
     System.out.println("Feth bean B.");
     context.getBean("testB");
   }
}

Output

Bean A is initialized
Feth bean B.
Bean B is initialized

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow