在实际使用 Spring/Spring Boot 开发中,一些 Bean 在初始化过程中执行准备操作,如拉取远程配置、初始化数据源等等。在应用启动期间,这些 Bean 会增加 Spring 上下文刷新时间,导致应用启动耗时变长。

在工程的 文件中,引入如下 starter:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  4. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  5. http://sofastack.io/schema/sofaboot http://sofastack.io/schema/sofaboot.xsd"
  6. <!-- async init test -->
  7. <bean id="testBean" class="com.alipay.sofa.runtime.beans.TimeWasteBean" init-method="init" async-init="true"/>
  8. </beans>

SOFABoot 异步初始化能力提供两个属性配置,用于指定负责异步执行 Bean 初始化方法(init-method)的线程池大小:

  • com.alipay.sofa.boot.asyncInitBeanCoreSize:线程池基本大小,默认值为 CPU 核数加一。