The Spring bean Lazy loading annotation example show you how to use @Lazy annotation together with @Component and @Autowired annotation. Spring container is eagerly loading all autowired dependencies at startup by default. In case, we want to mark a bean is lazy loading, we have to annotated @Lazy annotation on the bean as well at the point where it is @Autowired in. This is because if you don’t have @Lazy on the bean, then it is eagerly created, and if you have don’t have an @Lazy on the @Autowired then again it is eagerly created and injected into the bean.
Decclare @Lazy and @Autowired annotation
In this example, we create two beans named University and Student. The University bean is annotated with the @Lazy annotation, which make a bean is to be lazily initialized.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package com.javabycode.spring.core.lazy; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; @Lazy @Component public class University { public University() { System.out.println("University initialized"); } @Override public String toString() { return "University{}"; } } |
The Student bean will be created eagerly and autowired the University bean. But the autowired University bean is annotated with the @Lazy annotation too, which makes the Spring container to initialize the bean at first request.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.javabycode.spring.core.lazy; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; @Component public class Student { private @Autowired @Lazy University univeristy; public Student() { System.out.println("Student initialized"); } public University getUniversity() { return univeristy; } } |
Spring Configuration
This example is using annotations and components so we need to enable component scanning and annotation configuration by adding the
DEMO
We create the main class and print messages which indicate what bean is loading and when.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package com.javabycode.spring.core.lazy; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MySpringApplication { public static void main(String... args) throws InterruptedException { ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); System.out.println("Application context loaded"); System.out.println("Getting Student Bean"); Student student = context.getBean(Student.class); System.out.println("Getting the university"); University university = student.getUniversity(); System.out.println(university.toString()); } } |
Running the above main and the output is printed like below:
Look at the output, you see the University bean is lazy loaded.
That’s all on the Spring bean Lazy loading annotation example.
References
@Lazy JavaDoc
Spring bean Lazy loading xml
Download complete source code, click link below
spring-bean-lazy-loading-annotation-example.zip (201 downloads)