Spring - @Lazy annotation example

Posted on January 9, 2017


Technologies used: JDK 1.8.0_71 | Spring 4.3.5.RELEASE | Maven 3.3.9 | Eclipse Mars.2 (4.5.2)

By default, the Spring IoC container creates and initializes all singleton beans at time of application start up. We can prevent this pre initialization of a singleton beans by using the @Lazy annotation.

The @Lazy annotation may be used on any class directly or indirectly annotated with @Component or on methods annotated with @Bean.

This post describes how to use @Lazy annotation in spring application.

@Lazy + @Bean annotation example

Consider the following beans BeanOne and BeanTwo.

BeanOne.java

package com.boraji.tutorial.spring;

public class BeanOne {

   public BeanOne() {
      System.out.println("BeanOne Initialized");
   }

   public void doSomthing() {
      System.out.println("Inside doSomthing() method of BeanOne");
   }
}

BeanTwo.java

package com.boraji.tutorial.spring;

public class BeanTwo {

   public BeanTwo() {
      System.out.println("BeanTwo Initialized");
   }

   public void doSomthing() {
      System.out.println("Inside doSomthing() method of BeanTwo");
   }
}

Declare the above beans in java based configuration class as show below.

AppConfig.java

package com.boraji.tutorial.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

@Configuration
public class AppConfig {

   @Bean("beanOne")
   @Lazy(value = true)
   public BeanOne getBeanOne() {
      return new BeanOne();
   }

   @Bean("beanTwo")
   public BeanTwo getBeanTwo() {
      return new BeanTwo();
   }
}

Create main class and run application.

MainApp.java

package com.boraji.tutorial.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MainApp {
   public static void main(String[] args) {
      AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
      //Initialize lazy bean
      BeanOne bean=context.getBean(BeanOne.class);
      bean.doSomthing();
      context.close();
   }
}

Output

BeanTwo Initialized
BeanOne Initialized
Inside doSomthing() method of BeanOne

As we can see, bean beanTwo is initialized by Spring container while bean beanOne is initialized explicitly.

 

@Lazy + @Component example 

Consider the following beans BeanThree and BeanFour annotated with @Component annotation.

BeanThree.java

package com.boraji.tutorial.spring;

import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

@Component
@Lazy
public class BeanThree {

   public BeanThree() {
      System.out.println("BeanThree Initialized");
   }

   public void doSomthing() {
      System.out.println("Inside doSomthing() method of BeanThree");
   }
}

BeanFour.java

package com.boraji.tutorial.spring;

import org.springframework.stereotype.Component;

@Component
public class BeanFour {

   public BeanFour() {
      System.out.println("BeanFour Initialized");
   }

   public void doSomthing() {
      System.out.println("Inside doSomthing() method of BeanFour");
   }
}

Create a java based configuration as follow.

AppConfig.java

package com.boraji.tutorial.spring;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages="com.boraji.tutorial.spring")
public class AppConfig {

}

The @ComponentScan annotation scans all beans whose class is annotated by @Component annotation in a package specified by basePackages attribute.

Now create main class and run application.

MainApp.java

package com.boraji.tutorial.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MainApp {
   public static void main(String[] args) {
      AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
      
    //Initialize lazy bean
     BeanThree bean2=context.getBean(BeanThree.class);
     bean2.doSomthing();
      
      context.close();
   }
}

 Output

BeanFour Initialized
BeanThree Initialized
Inside doSomthing() method of BeanThree