SOFABoot 在 v2.6.0 开始提供异步初始化 Spring Bean 能力,引入如下 Starter 即可:

异步初始化 Bean 的原理是开启单独线程负责执行 Bean 的初始化方法(init-method),因此在使用过程中,除了引入上述依赖管理,还需要在 Bean 的 xml 定义中配置 属性,用于指定是否异步执行该 Bean 的初始化方法,例如:

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

配置可以通过 VM -D 参数或者 Spring Boot 配置文件 application.yml 设置。