Spring @Lazy Annotation Example
November 09, 2021
This page will walk through Spring @Lazy
annotation example.
1. Spring
@Lazy
annotation indicates that a bean will be lazily initialized. The @Lazy
can be annotated with @Component
, @Configuration
or on methods annotated with @Bean
.
2. The default scope of a bean is singleton scope. Generally singleton beans are pre-initialized to discover errors in the configuration or surrounding environment immediately.
3. To initialize a bean lazily we can use
@Lazy
annotation in JavaConfig based application or use lazy-init
attribute on the <bean>
element in XML configuration based application.
4. Lazy initialization of a bean means a bean will not be initialized until referenced by another bean or explicitly retrieved from the enclosing
BeanFactory
.
5. By default, beans are eagerly initialized. Eager initialization means that the bean will be instantiated on startup by bean factories. If
@Lazy
annotation is used with false value as @Lazy(value = false)
, bean will be eagerly initialized.
6. If
@Lazy
annotation is present on class level with @Configuration
annotation, all the @Bean
methods of that class are lazily initialized. If a bean in this class is annotated with @Lazy(value = false)
, that bean method is eagerly initialized.
7. The
@Lazy
annotation can also be used with @Autowired
or @Inject
annotation.
8. The
@Lazy
annotation is introduced in Spring 3.0.
@Lazy with @Configuration
The@Lazy
can be used with @Configuration
and this makes all beans of this configuration class to be lazy initialized.
Find the code.
AppConf.java
package com.concretepage; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Lazy; @Lazy @Configuration public class AppConf { @Bean public User user() { return new User("Ram"); } @Bean public Book book() { return new Book("Ramayan"); } }
package com.concretepage; public class User { private String userName; public User(String userName) { System.out.println("--- User Instantiated ---"); this.userName = userName; } public String getUserName() { return userName; } }
package com.concretepage; public class Book { private String bookName; public Book(String bookName) { System.out.println("--- Book Instantiated ---"); this.bookName = bookName; } public String getBookName() { return bookName; } }
package com.concretepage; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class SpringDemo { public static void main(String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(AppConf.class); ctx.refresh(); User user = ctx.getBean(User.class); System.out.println("User Name:" + user.getUserName()); Book book = ctx.getBean(Book.class); System.out.println("Book Name:" + book.getBookName()); ctx.registerShutdownHook(); ctx.close(); } }
19:14:29.386 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'user' --- User Instantiated --- User Name:Ram 19:14:29.426 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'book' --- Book Instantiated --- Book Name:Ramayan
@Lazy with @Bean
The@Lazy
can be used with @Bean
in Java configuration class. The beans annotated with @Lazy
annotation will be lazily initialized.
@Configuration public class AppConf { @Lazy @Bean public User user() { return new User("Ram"); } @Lazy @Bean public Book book() { return new Book("Ramayan"); } }
@Lazy
i.e. @Lazy(value = false)
or @Lazy(false)
, the bean will be eagerly initialized.
Suppose we have a configuration class annotated with
@Lazy
at class level. Then all beans of this class will be lazily initialized automatically. If we want some beans of this configuration class to be eagerly initialized, we can use @Lazy(false)
with @Bean
annotation.
@Lazy @Configuration public class AppConf { @Bean public User user() { return new User("Ram"); } @Lazy(false) @Bean public Book book() { return new Book("Ramayan"); } }
book
bean will be eagerly initialized and user
bean will be lazily initialized.
@Lazy with @Autowired
The@Lazy
annotation can be used with Autowired
or Inject
annotation that leads to the creation of a lazy-resolution proxy for all affected dependencies.
Find the code.
AppConf.java
@Configuration @ComponentScan("com.concretepage") public class AppConf { }
@Lazy
can be used with @Component
, @Service
etc.
Create a component annotated with
@Component
and @Lazy
annotation.
MyUtility.java
@Lazy @Component public class MyUtility { public MyUtility() { System.out.println("--- MyUtility Instantiated ---"); } public int add(int n1, int n2) { return n1 + n2; } }
@Lazy
annotation with @Autowired
.
SpringAppTest.java
@ExtendWith(SpringExtension.class) @ContextConfiguration(classes = AppConf.class) public class SpringAppTest { @Lazy @Autowired private MyUtility utility; @Test public void test() { assertEquals(50, utility.add(20, 30)); } }