spring中使用@DependsOn注解控制bean的加载顺序

2 篇文章 0 订阅
2 篇文章 0 订阅

错误回溯

获取spring的上下文,放到静态变量applicationContext

@Component
public class ApplicationContextUtil implements ApplicationContextAware {
    public static ApplicationContext applicationContext = null;
    @Override
    public void setApplicationContext(ApplicationContext args){
        applicationContext = args;
    }
}

缓存组件,使用单例模式,没有放到容器中

public class CacheComponent {
    List<IotOperatorTemplate> iotOperatorTemplates;
    private CacheComponent(){
    //注意,此处使用了上一步获取的上下文属性applicationContext
        IotOperatorTemplateMapper iotOperatorTemplateMapper = (IotOperatorTemplateMapper)ApplicationContextUtil.applicationContext.getBean("iotOperatorTemplateMapper");
        iotOperatorTemplates = iotOperatorTemplateMapper.selectList(null);
    }
    public static class CacheHolder{
       private static CacheComponent cacheComponent =  new CacheComponent();
    }
    public static CacheComponent getInstance(){
        return CacheHolder.cacheComponent;
    }
}

容器中的bean,项目启动就获取缓存组件中的缓存对象

@Component
public class ThirdHuClient implements InitializingBean {

    @Override
    public void afterPropertiesSet(){
        try{
            //从缓存组件获取所有运营商模板
            IotOperatorTemplate iotOperatorTemplate = CacheComponent.getInstance().getIotOperatorTemplates()
                    .stream().filter(item -> item.getType().equals(TemplateConstants.THRID_HU.getMessage())).findFirst().get();
    }
   }
}     

然后有service自动注入了ThirdHuClient,项目启动,报错如下图
在这里插入图片描述
分析上图发现,当容器启动时,首先调用了ThirdHuClientafterPropertiesSet()方法,该方法中CacheComponent.getInstance()触发单例类加载
在这里插入图片描述
在这里插入图片描述

原因分析

CacheComponent类加载时,此时ApplicationContextUtil还没有放入到容器,故获取不到应用上下文,所以调用getBean()方法会抛出NPE。

解决

ThirdHuClient这个bean的初始化必须依赖ApplicationContextUtil这个bean,此时主角就出场了,@DependsOn,在ThirdHuClient类上加上这个注解,表示先初始化ApplicationContextUtil。如图所示
在这里插入图片描述

结论

有很多场景需要bean B应该被先于bean A被初始化,从而避免各种负面影响。我们可以在bean A上使用@DependsOn注解,告诉容器bean B应该先被初始化

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值