×
☰ See All Chapters

Spring @Lazy Annotation

The bean which has been declared with @Lazy annotation will not be initialized by spring container during start-up/bootstrapping of the application context. @Lazy will be initialized by container only when that bean will be accessed somewhere in code. @Lazy Annotation in spring is used with @Configuration annotation. If @Lazy Annotation is used there is a chance of getting runtime exception if spring has problem creating bean. Unless it is necessary and confident about bean creation, it is not recommended to use @Lazy, you can detect all possible bean creation errors at application start-up, rather than at runtime. When your bean is involving large objects like holding large amount of data from database, it is not good to keep such large data object ready in memory till its use. Also if @Lazy loading is not used, it may take more time to start the application, where it has to hit database and load large data to bean. In such cases we should use @Lazy annotation. Now, hope you have understood when to use @Lazy annotation.

Now let us see an example how @Lazy annotation works.

Spring @Lazy Annotation Example

pom.xml

<project xmlns="https://maven.apache.org/POM/4.0.0"

        xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

        <modelVersion>4.0.0</modelVersion>

        <groupId>com.java4coding</groupId>

        <artifactId>Spring3_Lazy</artifactId>

        <version>0.0.1-SNAPSHOT</version>

 

        <dependencies>

                <dependency>

                        <groupId>org.springframework</groupId>

                        <artifactId>spring-core</artifactId>

                        <version>${spring.version}</version>

                </dependency>

                <dependency>

                        <groupId>org.springframework</groupId>

                        <artifactId>spring-context</artifactId>

                        <version>${spring.version}</version>

                </dependency>

                <dependency>

                        <groupId>org.springframework</groupId>

                        <artifactId>spring-aspects</artifactId>

                        <version>${spring.version}</version>

                </dependency>

        </dependencies>

 

        <properties>

                <spring.version>4.2.4.RELEASE</spring.version>

        </properties>

 

</project>

Mango.java

package com.java4coding;

 

public class Mango {

        public Mango() {

                System.out.println("Mango bean instantiated");

        }

}

Apple.java

package com.java4coding;

 

public class Apple{

        public Apple() {

                System.out.println("Apple bean instantiated");

        }

}

SpringConfiguration.java

package com.java4coding;

 

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.Lazy;

 

@Configuration

public class SpringConfiguration {

       

        @Bean

    public Mango getMango() {

        return new Mango();

    }

       

    @Bean

    @Lazy

    public Apple getApple() {

        return new Apple();

    }

 

}

Demo.java

package com.java4coding;

 

import org.springframework.context.ApplicationContext;

import org.springframework.context.ConfigurableApplicationContext;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

 

public class Demo {

        public static void main(String[] args) {

                ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfiguration.class);

                ((ConfigurableApplicationContext)context).close();

        }

}

Project Directory Structure

spring-lazy-annotation-0
 

Output:

In configuration class we have used @Lazy annotation for Apple bean; as a result we can see only Mango bean creation during start-up.

spring-lazy-annotation-1
 

Now we shall apply @Lazy for both Mango and Apple bean, and below is the output:

spring-lazy-annotation-2
 

When @Lazy is removed from all the beans, below is the output:

spring-lazy-annotation-3
 

All Chapters
Author