About lazy loading
In Spring, all specified beans and their dependencies are initialized by default when the container context is created at application startup. The test code is as follows:
@Slf4j
@Configuration
public class DemoConfig {
public DemoConfig(a) {
log.warn("> > > demoConfig initialized > > >");
} } Copy the code
Start application logs:
[ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
[ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1193 ms
[the main] C.P.C.G lobal. Lazy. Config. DemoConfig: > > > DemoConfig 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 '' Copy the code
The DemoConfig bean has been initialized before Tomcat started.
Generally, there are a large number of beans that need to be initialized at startup, such as data source initialization, cache initialization, etc., resulting in a very slow startup of the application. Prior to Spring Boot 2.2, we manually added the “@lazy” annotation to these beans, so that they are not initialized at startup and the business program will initialize them when it needs to be called.
@Lazy
@Configuration
public class DemoConfig {}
Copy the code
Why global lazy loading
As mentioned above, we need to manually add the “@lazy” annotation to our beans, which means that we can only add beans that we implement ourselves. However, spring Boot applications now introduce many third-party starter, such as druid-spring-boot-starter data source injection, spring-boot-starter-data-redis cache, and so on. Importing injects related beans that we cannot modify to add “@lazy”.
- Spring Boot 2.2 added the global lazy loading attribute. When enabled, global beans are set to lazy loading and can be created as needed
spring:
main:
lazy-initialization: true # Default false To disable
Copy the code
- Individual beans can be excluded by setting @lazy (false) to load at startup
@Lazy(false)
@Configuration
public class DemoConfig {}
Copy the code
- Of course you can also specify rule implementation LazyInitializationExcludeFilter rule implementation
@Bean
LazyInitializationExcludeFilter integrationLazyInitExcludeFilter(a) {
return LazyInitializationExcludeFilter.forBeanTypes(DemoConfig.class);
}
Copy the code
Global lazy loading
By setting global lazy loading, we can significantly reduce the startup time of the application by reducing the creation task at startup. However, the disadvantages of global lazy loading can be summarized as the following two points:
-
The PROCESSING time of Http requests becomes longer. The first HTTP request will take longer to process, and subsequent requests will not be affected (which naturally relates to the timeout of the first call after Spring Cloud launches).
-
Errors are not thrown when the application is started, which is not conducive to early detection, early resolution, and early dismissal.
conclusion
- Above source code:spring-boot-course