New features spring boot global lazy loading mechanism


About delayed loading

In spring, by default, all defined beans and their dependent projects are created when the application starts, and the container context is initialized. The test code is as follows:

public class DemoConfig {
    public DemoConfig() {
        log.warn ("> > > democonfig is initialized > > >);

Start application log:

[           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
[           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1193 ms
[           main] c.p.c. global.lazy.config . democonfig: > > > democonfig is initialized > > >
[           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
[           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''

The above log: democonfig bean has been initialized before Tomcat started.

In general, a large number of beans need to be initialized when the program is started, such as data source initialization, cache initialization, etc., which causes the application to start very slowly. Prior to spring boot 2.2, we added these beans manually@LazyNote to realize that the business program is not initialized when it is started, and the business program needs to be initialized when it is called. The above code can be modified as follows:

public class DemoConfig {}

Why do we need global lazy loading

As mentioned above, we need to manually add to the bean@LazyAnnotation, which means that we can only add self implemented beans in the program. However, many third-party starters have been introduced into spring boot applications, such as Druid spring boot starter data source injection, spring boot starter data redis cache, etc. by default, when the relevant beans are introduced, we cannot modify and add them@Lazy

  • Spring boot 2.2 adds the global lazy load property. After it is enabled, the global bean is set to lazy loading and can be created when necessary
    Lazy initialization: true ා false by default
  • Individual beans can be set by@Lazy(false)When loading, set to exclude
public class DemoConfig {}
  • Of course, you can also specify rules to implement lazyinitializationexcludefilter rules to implement exclusion
 LazyInitializationExcludeFilter integrationLazyInitExcludeFilter() {
    return LazyInitializationExcludeFilter.forBeanTypes(DemoConfig.class);

The problem of global lazy loading

By setting global lazy loading, we can reduce the creation task at startup, thus greatly reducing the application startup time. However, the disadvantages of global lazy loading can be summarized as follows:

  • HTTP requests take longer to process. To be exact, the processing time of the first HTTP request becomes longer, and the subsequent requests will not be affected (speaking of this, it will naturally be related to the timeout problem of the first call after the spring cloud is started).
  • Errors will not be thrown when the application starts, which is not conducive to early discovery, early resolution and early off-duty.


New features spring boot global lazy loading mechanism

New features spring boot global lazy loading mechanism

  • Above source code: spring boot course

New features spring boot global lazy loading mechanism