In this quick article, we’ll discuss Spring’s @Lazy annotation with an example.
By default, the Spring IoC container creates and initializes all singleton beans at time of application startup. We can prevent this pre-initialization of a singleton bean 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. In this example, we will use a Java-based configuration(using @Configuration and @Bean).
Spring @Lazy Annotation Example
Let's create an example to demonstrates usage of use @Lazy annotation in a spring application.
Tools and technologies used
- Spring Framework - 5.1.0.RELEASE
- JDK - 8 or later
- Maven - 3.2+
- IDE - Eclipse Mars/STS
Create a Simple Maven Project
Create a simple maven project using your favorite IDE and refer below section for packaging structure. If you are new to maven then read this article How to Create a Simple Maven Project.
Project Structure
Below diagram shows a project structure for your reference -
The pom.xml File
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.javaguides.spring</groupId>
<artifactId>spring-lazy-annotation</artifactId>
<version>0.0.1-SNAPSHOT</version>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.0.RELEASE</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Create Spring Beans - FirstBean and SecondBean
FirstBean.java
package net.javaguides.spring.lazy;
public class FirstBean {
public FirstBean() {
System.out.println("Inside FirstBean Constuctor");
}
public void test() {
System.out.println("Method of FirstBean Class");
}
}
SecondBean.java
package net.javaguides.spring.lazy;
public class SecondBean {
public SecondBean() {
System.out.println("Inside SecondBean Constuctor");
}
public void test() {
System.out.println("Method of SecondBean Class");
}
}
Java Based Configuration - AppConfig.java
Declare the above beans in java based configuration class.
package net.javaguides.spring.lazy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
@Configuration
public class AppConfig {
@Lazy(value = true)
@Bean
public FirstBean firstBean() {
return new FirstBean();
}
@Bean
public SecondBean secondBean() {
return new SecondBean();
}
}
Running Spring Application - Application.java
Let's create a main class and run an application.
package net.javaguides.spring.lazy;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Application {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
FirstBean firstBean = context.getBean(FirstBean.class);
firstBean.test();
context.close();
}
}
Output
Inside SecondBean Constuctor
Inside FirstBean Constuctor
Method of FirstBean Class
As we can see, bean secondBean is initialized by Spring container while bean firstBean is initialized explicitly.
The source code of this article is available on my GitHub repository https://github.com/RameshMF/spring-core-tutorial
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course
Comments
Post a Comment