SpringBoot @Lazy Annotation with Example:

In our application we might dont want to initialize all bean when the springboot application starts. We might want to initialize certain beans only when it is required.

In order to initialize the bean only when it is required we use an annotation @Lazy

To notify the spring boot application to initialize the bean as Lazy, we need to declare this annotation in @Component or @Bean definition. If this annotation is not present, spring boot will follow eager initialization.

@Lazy annotation can also be used in @Configuration config class. If @Lazy is at Configuration class then it means all the beans declared in configuration class should be lazily initialized. It is possible to declare @Lazy for specific bean to within Configuration class.

When declaring the @Lazy annotation for specific @Bean, we  need to pass the value as true or false. @Lazy (value = “true”).

Now let us see an example,

We are going to create to model classes, Student and Employee. We are going to have Configuration class to declare these beans.

pom.xml

<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>SpringBootBeans</groupId>
	<artifactId>SpringBootBeans</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>SpringBootBeans</name>
	<url>http://maven.apache.org</url>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.4.2.RELEASE</version>
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>
</project>

 

Employee.java

package com.javainfinite.beans;

public class Employee {
	
	public Employee() {
		System.out.println("Employee Initialized");
	}
	
	int id;
	
	String name;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}	
}

Student.java

package com.javainfinite.beans;

public class Student {

	public Student() {
		System.out.println("Student Bean Initialization");
	}

	int id;
	String name;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

Config.java

package com.javainfinite.configuration;

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

import com.javainfinite.beans.Employee;
import com.javainfinite.beans.Student;

@Configuration
public class Config {

	@Bean
	public Employee employee() {
		return new Employee();
	}

	@Bean
	@Lazy(value = true)
	public Student student() {
		return new Student();
	}

}
package com.javainfinite.main;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

/**
 * Hello world!
 *
 */
@SpringBootApplication
@ComponentScan("com.javainfinite")
public class App 
{
    public static void main( String[] args )
    {
    	SpringApplication.run(App.class, args);
    }
}

Output:

From the output we can see Employee got initialized but Student is not initialized when the application starts.

Download Code here

By Sri

Leave a Reply

Your email address will not be published. Required fields are marked *