Guide to the Spring @Lazy Annotation

Spring @Lazy annotation

In this article, we will cover and discuss Spring @Lazy annotation.

 

Introduction

By default, Spring IoC creates and initializes all singleton beans at time of application startup.This default behavior ensures that any possible error caught immediately.This feature is really good to avoid any runtime errors but there are a number of use cases when we do not want Spring IoC to create beans on startup but create it when requested by the application.Spring @Lazy annotation may be used to prevent pre-initialization of the singleton beans.

 

1. Spring @Lazy Annotation

The @Lazy annotation is available with Spring framework with version 3.0.This annotation used on any class directly or indirectly annotated with @Component or on methods annotated with @Bean.

 

1.1. @Configuration Class Level Annotation

If @Lazy is present in a @Configuration class, this indicates that all @Bean methods within that @Configuration should be lazily initialized.

@Lazy
@Configuration
@ComponentScan(basePackages = "com.javadevjournal.rest")
public class LazyAppConfig {
    
  @Bean
  public CustomerService customerService(){
    return new CustomerService();
  }
    
  @Bean
  public UserService userService(){
    return new UserServiceImpl();
  }
}

In this case, all the beans defined under LazyAppConfig class initiated only when requested the first time.To Test this, we can run a simple unit test case.

@Test
public void test_lazy_bean_init(){

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(LazyAppConfig.class);
context.refresh();
context.getBean(CustomerService.class);
context.getBean(UserService.class);
}

 

1.2. Bean Specific Lazy Initialization

We have the option to define/declare @Lazy annotation at the bean level.@Lazy is present and false on a @Bean method within a @Lazy-annotated @Configuration class, this indicates overriding the ‘default lazy’ behavior and that the bean be eagerly initialized.

@Configuration
@ComponentScan(basePackages = "com.javadevjournal.rest")
public class LazyAppConfig {
    
  @Lazy(true) //true is the default value.
  @Bean
  public CustomerService customerService(){
    return new CustomerService();
  }
    
  @Lazy(false)
  @Bean
  public UserService userService(){
    return new UserServiceImpl();
  }
}

 

1.3. @Autowired or @Inject level Annotation

We may use @Lazy annotation on with @Autowired or @Inject annotation.When placed on these annotations, it leads to the creation of a lazy-resolution proxy for all affected dependencies.

@Lazy
@Component
public class OrderService {
//business logic
}

Our OrderFacade using OrderService.

public class OrderFacade {
  
  @Lazy
  @Autowired
  private OrderService orderService;

  public OrderDto getOrderById(String id){
       return convertToOrderDto(orderService.getOrderById(id));
    }
}

 

Summary

In this post, we covered different features of the Spring @Lazy annotation.We learned how to control the eager initialization of Spring singleton beans along with different options to configure and use @Lazy annotation.