优化 springboot 启动速度
前言: springboot
的starter
功能很强大,但是也导致了springboot
项目启动缓慢。在一定场景下,我们可能需要springboot
项目尽量小,启动速度尽可能的快。
分析原因:
默认情况下, 我们会使用@SpringBootApplication
注解来自动获取应用的配置信息. 这样会有一些副作用. 其中一个就是组件扫描@ComponentScan . 它会拖慢应用启动的速度, 也会加载一些不必要的bean.
改进方法:
1.不使用@SpringBootApplication
,用 @Configuration
和@EnableAutoConfiguration
代替
/**
* @author cphovo
*/
@Configuration
@EnableAutoConfiguration
// @SpringBootApplication
public class MiniSpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(MiniSpringbootApplication.class, args);
}
}
2.但是这样也引发了问题 -> 无法扫描到我们编写的组件类,如 @Controller
、@Service
、@Mapper
等标记的组件无法被扫描到。
解决办法:用@Bean
显式进行配置这些类,但是要注意依赖关系,被依赖的需要先配置
@Configuration
public class BeansConfig {
@Resource
private SqlSessionFactory sqlSessionFactory;
@Bean
public MapperFactoryBean<UserMapper> userMapper() {
MapperFactoryBean<UserMapper> mapperFactoryBean = new MapperFactoryBean<>();
mapperFactoryBean.setMapperInterface(UserMapper.class);
mapperFactoryBean.setSqlSessionFactory(sqlSessionFactory);
return mapperFactoryBean;
}
@Bean
public UserServiceImpl userService() {
return new UserServiceImpl();
}
@Bean
public UserController userController() {
return new UserController();
}
}
进一步优化
使用 @Configuration
和@EnableAutoConfiguration
其实还有可以优化启动速度的地方,@EnableAutoConfiguration
会扫描所有 @Configuration
的组件,这也导致启动速度变慢。
1.在application.properties
配置logging.level.org.springframework.boot.autoconfigure=DEBUG
,查看哪些是必要的Bean
============================
CONDITIONS EVALUATION REPORT
============================
Positive matches:
-----------------
AopAutoConfiguration matched:
- @ConditionalOnProperty (spring.aop.auto=true) matched (OnPropertyCondition)
AopAutoConfiguration.ClassProxyingConfiguration matched:
- @ConditionalOnMissingClass did not find unwanted class 'org.aspectj.weaver.Advice' (OnClassCondition)
- @ConditionalOnProperty (spring.aop.proxy-target-class=true) matched (OnPropertyCondition)
DataSourceAutoConfiguration matched:
- @ConditionalOnClass found required classes 'javax.sql.DataSource', 'org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType' (OnClassCondition)
- @ConditionalOnMissingBean (types: io.r2dbc.spi.ConnectionFactory; SearchStrategy: all) did not find any beans (OnBeanCondition)
DataSourceAutoConfiguration.PooledDataSourceConfiguration matched:
- AnyNestedCondition 1 matched 1 did not; NestedCondition on DataSourceAutoConfiguration.PooledDataSourceCondition.PooledDataSourceAvailable PooledDataSource found supported DataSource; NestedCondition on DataSourceAutoConfiguration.PooledDataSourceCondition.ExplicitType @ConditionalOnProperty (spring.datasource.type) did not find property 'type' (DataSourceAutoConfiguration.PooledDataSourceCondition)
- @ConditionalOnMissingBean (types: javax.sql.DataSource,javax.sql.XADataSource; SearchStrategy: all) did not find any beans (OnBeanCondition)
...
- 关注
Positive matches
下的AutoConfiguration
,把需要的自动配置用@Import
显示引入。
@Configuration
@Import({
DataSourceAutoConfiguration.class,
DispatcherServletAutoConfiguration.class,
ErrorMvcAutoConfiguration.class,
HttpEncodingAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
JacksonAutoConfiguration.class,
MybatisAutoConfiguration.class,
ServletWebServerFactoryAutoConfiguration.class,
WebMvcAutoConfiguration.class,
BeansConfig.class
})
//@EnableAutoConfiguration
//@SpringBootApplication
public class MiniSpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(MiniSpringbootApplication.class, args);
}
}
2.使用springboot
全局懒加载,设置spring.main.lazy-initialization=true
3.关闭springboot JMX
监控,设置spring.jmx.enabled=false
4.设置JVM
参数-noverify
,不对类进行验证