SoFunction
Updated on 2025-04-10

Tips and example analysis of @Lazy annotation in Spring

In Spring framework,@LazyAnnotations are a very practical feature that can help us delay the initialization time of the bean, thereby optimizing the startup performance of the application. Starting with Spring 4.0.0,@LazyNot only can be applied to@Beanor@Component, can also directly act on the injection point (e.g.@Autowired@Injector@Resource). This means that we can control the initialization timing of the bean more flexibly and avoid unnecessary resource consumption.

1. The role of @Lazy annotation

(I) Delayed Bean Initialization

In Spring, by default, all beans are initialized when the container starts. However, some beans may not need to be loaded immediately upon startup, but can be initialized only when needed.@LazyThe annotation is designed to solve this problem. It can delay the initialization of the bean until the first time it is used, thereby reducing resource consumption at application startup.

(II) Used in combination with @Autowired

when@Lazyand@AutowiredWhen used together, it delays the initialization of the dependency bean until the dependency is actually called. This usage is very useful when dealing with complex dependencies and avoids unnecessary initialization overhead.

2. Example analysis

To better understand@LazyWe demonstrate the effect of this through a simple Spring project.

(I) Code Example

1. Normally initialized bean

package ;
import ;
import ;
import ;
public class MyEagerBean {
    @Autowired
    @Lazy
    private MyLazyBean myLazyBean;
    @PostConstruct
    public void init() {
        (getClass().getSimpleName() + " has been initialized");
    }
    public void doSomethingWithLazyBean() {
        ("Using lazy bean");
        ();
    }
}

2. Delayed initialization bean

public class MyLazyBean {
    @PostConstruct
    public void init() {
        (getClass().getSimpleName() + " has been initialized");
    }
    public void doSomething() {
        ("inside lazy bean doSomething()");
    }
}

3. Main category

public class LazyExampleMain {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext();
        ("--- container initialized ---");
        MyEagerBean bean = ();
        ("MyEagerBean retrieved from bean factory");
        ();
    }
}

(II) Operation results

When running the above code, the output is as follows:

MyEagerBean has been initialized
--- container initialized ---
MyEagerBean retrieved from bean factory
Using lazy bean
MyLazyBean has been initialized
inside lazy bean doSomething()

From the output, you can see thatMyLazyBeanInitialization is delayed toMyEagerBeanCalldoSomethingWithLazyBean()when.

(III) Comparative Test

If we will@LazyAnnotation fromMyEagerBeanRemoved in, i.e.:

public class MyEagerBean {
    @Autowired
    //@Lazy
    private MyLazyBean myLazyBean;
    ...
}

The result of the operation will become:

MyLazyBean has been initialized
MyEagerBean has been initialized
--- container initialized ---
MyEagerBean retrieved from bean factory
Using lazy bean
inside lazy bean doSomething()

at this time,MyLazyBeanIt is initialized when the container starts, rather than delayed until the first use.

3. How @Lazy works

when@LazyWhen annotations are applied to the injection point, Spring creates a proxy object for the injection point that is delayed parsing, instead of directly initializing the target bean. Spring will only really initialize the bean when it is actually called.

4. Summary

@LazyAnnotations are a very practical feature in the Spring framework. They can help us delay the initialization of beans and optimize the startup performance of our application. By@AutowiredWith the combination of annotations, we can load the beans only when needed to avoid unnecessary resource consumption. In actual development, use it reasonably@LazyIt can significantly improve the performance and maintainability of the application.

This is the article about the usage skills and example analysis of @Lazy annotations in Spring. For more related Spring @Lazy annotations, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!