SpringBoot-Lazy-Initialization
在SpringBoot-Testing提到,使用Junit
测试SpringBoot
项目时,需要使用@SpringBootTest
来创建一个Spring容器
。而当@SpringBootTest
标注的测试类没有使用内嵌@Configuration
注解时,会自动使用@SpringBootConfiguration
上的配置。
Automatically searches for a @SpringBootConfiguration when nested @Configuration is not used, and no explicit classes are specified.
@SpringBootConfiguration
@SpringBootConfiguration
本身是一个合成注解,包含三个注解:
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
其中,@SpringBootConfiguration
本身也包含@Configuration
,也就是可以在类中通过@bean
注解往容器中添加bean
。@EnableAutoConfiguration
为自动配置功能,可以根据pom文件中的依赖,自动向容器中添加bean
。@ComponentScan
组件扫描在SpringBoot-Configuration已经阐述过。
测试往往只针对某个新写的方法,但默认的配置需要将整个应用程序环境都启动,会导致测试程序启动缓慢。一个很直观的解决方法就是使用自定义的配置类,而不是使用默认的主应用的程序环境。
自定义配置类
前面提到@SpringBootTest
标注如果带有内嵌@Configuration
注解,则不会使用@SpringBootConfiguration
上的配置。因此如果需要使用自定义配置类,需要在测试类上添加@Configuration
或通过@SpringBootTest
的classes
属性指定自定义配置类。
1 |
|
内嵌自定义配置类需要定义在测试类中,并且使用static
关键词修饰。
static, non-private, non-final, nested classes annotated with @Configuration
1 |
|
在自定义配置类中,可以如SpringBoot-Configuration中往容器中添加bean
懒加载
虽然Spring可以通过Auto-configured Tests
和@ComponentScan
来控制自动配置和包扫描规则,来避免不必要的bean导入。
1 |
|
但有时很难分清哪些被使用到,哪些没有被使用到,因为bean之间的依赖关系错综复杂。手动去控制自动配置规则和扫描规则太过繁琐。因此更直觉、简便的解决方法是使用懒加载。官方文档
SpringApplication
allows an application to be initialized lazily. When lazy initialization is enabled, beans are created as they are needed rather than during application startup. As a result, enabling lazy initialization can reduce the time that it takes your application to start.
懒加载就是在bean
被使用时才去实例化,而不是程序一启动就把所有的bean
都实例化。因此可以加快程序的启动时间,但会增加用户使用时的延迟。
对于生产中的设备来说,启动时间(只启动一次,往往在半夜没用户使用时)、内存消耗是可以忽略的,但用户体验至关重要。因此生产实践中通常不开启懒加载。
Lazy initialization can be enabled programmatically using the
lazyInitialization
method onSpringApplicationBuilder
or thesetLazyInitialization
method onSpringApplication
. Alternatively, it can be enabled using thespring.main.lazy-initialization
property
由于单元测试中容器是自动创建的,lazyInitialization
和setLazyInitialization
的方法无法执行,因此使用application.properties
配置文件的方式是最简单优雅的。
1 |
|
如果测试类需要使用独立的配置文件,可以在测试上使用@TestPropertySource
注解来指定。
1 |
|
总结
@SpringBootTest
标注的测试类,默认情况会使用@SpringBootConfiguration
的环境设置。
如果需要使用自定义配置可以使用内嵌@Configuration
或@SpringBootTest
的classes
属性。
懒加载是个加速程序启动的简单且有效方法。原理是将bean
的实例化过程推迟到使用时才实例化。避免了测试过程中大部分bean
都用不到,但会被实例化的问题。
在单元测试中,简单且优雅的方式是通过application.properties
配置文件来开启懒加载。为了避免影响生产,通常采用测试独立的配置文件,并通过@TestPropertySource
来指定。