Spring Bean循环引用如何简单识别?

2026-06-10 14:471阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计392个文字,预计阅读时间需要2分钟。

Spring Bean循环引用如何简单识别?

看了一次Spring公开课,记录一下bean的循环引用问题。

问题:在以下实例中,IndexService依赖IndexDao,而IndexDao又依赖IndexService,形成循环依赖。

javapublic class IndexService { @Autowired IndexDao indexDao;}

public class IndexDao { @Autowired IndexService indexService;}

看过一次spring公开课,记录一下bean的循环引用问题。

问题:

public class IndexService{   @Autowired   IndexDao indexDao; } public class IndexDao{   @Autowired   IndexService indexService; }

以上的实例中IndexService依赖IndexDao,IndexDao中依赖IndexService。

Spring Bean循环引用如何简单识别?

spring在bean的实例化过程:

先去创建IndexDao bean,

1.创建IndexDao实例,此时还没有IndexDao bean产生。

2.去配置IndexDao对象的属性,这个属性就是IndexService,在这个配置的过程中会先把自己(IndexDao)对象放到singleFactory中;

3.然后去查找IndexService bean去填充,发现单例池(专门存放bean)中没有,然后去singleFactory中去找,还是没有。

4.创建IndexService实例,此时还没有IndexService bean。

5.去配置IndexService对象的属性,这个属性就是IndexDao,在这个配置的过程中会先把自己(IndexDao)对象放到singleFactory中;

6.然后去查找IndexDao bean去填充,发现单例池中没有,然后去singleFactory中去找,发现有了。

7.然后把属性填充到IndexService中。

8.经过4,5,6,7后已经有了IndexService,并且此时属性IndexDao有值了,然后通过步骤3再将这个IndexService注入到IndexDao

9.然后继续完成IndexDao 后续的bean的初始化。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

本文共计392个文字,预计阅读时间需要2分钟。

Spring Bean循环引用如何简单识别?

看了一次Spring公开课,记录一下bean的循环引用问题。

问题:在以下实例中,IndexService依赖IndexDao,而IndexDao又依赖IndexService,形成循环依赖。

javapublic class IndexService { @Autowired IndexDao indexDao;}

public class IndexDao { @Autowired IndexService indexService;}

看过一次spring公开课,记录一下bean的循环引用问题。

问题:

public class IndexService{   @Autowired   IndexDao indexDao; } public class IndexDao{   @Autowired   IndexService indexService; }

以上的实例中IndexService依赖IndexDao,IndexDao中依赖IndexService。

Spring Bean循环引用如何简单识别?

spring在bean的实例化过程:

先去创建IndexDao bean,

1.创建IndexDao实例,此时还没有IndexDao bean产生。

2.去配置IndexDao对象的属性,这个属性就是IndexService,在这个配置的过程中会先把自己(IndexDao)对象放到singleFactory中;

3.然后去查找IndexService bean去填充,发现单例池(专门存放bean)中没有,然后去singleFactory中去找,还是没有。

4.创建IndexService实例,此时还没有IndexService bean。

5.去配置IndexService对象的属性,这个属性就是IndexDao,在这个配置的过程中会先把自己(IndexDao)对象放到singleFactory中;

6.然后去查找IndexDao bean去填充,发现单例池中没有,然后去singleFactory中去找,发现有了。

7.然后把属性填充到IndexService中。

8.经过4,5,6,7后已经有了IndexService,并且此时属性IndexDao有值了,然后通过步骤3再将这个IndexService注入到IndexDao

9.然后继续完成IndexDao 后续的bean的初始化。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。