Bean的异步初始化@SofaAsyncInit

异步注入核心类:AsyncInitBeanFactoryPostProcessor 核心类
其核心就是通过拦截器将标记为SofaAsyncInit注解的bean放入AsyncTaskExecutor异步线程池里面开启异步任务进行加载,所以会节省时间。但是如果两个bean之间注入是有关联的,那肯定不能异步执行只能一个注入了再注入另一个。
AsyncInitializeBeanMethodInvoker主要执行类
在这里插入图片描述
这里可以清楚的看到通过拿到注解,获取到方法、方法名、通过Spring的上下文事件监听模式,将Class文件进行反射加载。

举例:

<!-- 蚂蚁金服的bean的异步初始化组件-->
        <dependency>
            <groupId>com.alipay.sofa</groupId>
            <artifactId>runtime-sofa-boot-starter</artifactId>
            <version>3.18.0</version>
        </dependency>
public class config1 {

    public void init1() throws InterruptedException {
        Thread.sleep(6000);
        System.out.println("config1.init1注入完成耗时:6秒");
    }
}
public class config2 {

    public void init2() throws InterruptedException {
        Thread.sleep(5000);
        System.out.println("config2.init2注入完成耗时:5秒");
    }
}
import com.alipay.sofa.runtime.api.annotation.SofaAsyncInit;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class testBean {

    @Bean(initMethod = "init2")
    @SofaAsyncInit
    public config2 config2(){
        return new config2();
    }

    @Bean(initMethod = "init1")
    @SofaAsyncInit
    public config1 config1(){
        return new config1();
    }
}

大大节省注入时间

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值