Spring源码学习~循环依赖(面试必问系列!),守护线程面试题

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip1024b (备注Java)
img

正文

  •  A class
    
  • @author mazq

  • 修改记录

  • 修改后版本: 修改人: 修改日期: 2020/11/05 10:31 修改内容:

*/

@Component

public class A {

//@Autowired

B b;

public A() {

b = new B();

System.out.println(“A class is create”);

}

}

类B:

package com.example.bean;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;

/**

  •  B class
    
  • @author mazq

  • 修改记录

  • 修改后版本: 修改人: 修改日期: 2020/11/16 14:03 修改内容:

*/

@Component

public class B {

//@Autowired

A a;

public B() {

a = new A();

System.out.println(“B class is create”);

}

}

注册类A、B

package com.example.config;

import com.example.bean.B;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import com.example.bean.A;

/**

  •  AppConfiguration
    
  • @author mazq

  • 修改记录

  • 修改后版本: 修改人: 修改日期: 2020/11/05 10:26 修改内容:

*/

@Configuration

public class AppConfiguration {

@Bean

public A a(){

return new A();

}

@Bean

public B b() {

return new B();

}

}

package com.example;

import com.example.config.AppConfiguration;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.example.bean.A;

/**

  •  TestController
    
  • @author mazq

  • 修改记录

  • 修改后版本: 修改人: 修改日期: 2020/11/05 10:22 修改内容:

*/

public class TestApplication {

public static void testCircularReferences() {

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

context.register(AppConfiguration.class);

//context.setAllowCircularReferences(false);

context.refresh();

A bean = context.getBean(A.class);

System.out.println(bean);

}

public static void main(String[] args) {

// 测试Sprin循环依赖

testCircularReferences();

}

}

经过测试,一直在循环调用:

Spring源码学习~循环依赖(面试必问系列!)

4、循环依赖解决方法

==============

对于这种情况,Spring有处理方法?答案是有的,方法就是通过@Autowired注解,当然bean要是单例的,多例的情况不支持,原因后面分析

@Component

public class A {

@Autowired

B b;

public A() {

System.out.println(“A class is create”);

}

}

Spring源码学习~循环依赖(面试必问系列!)

补充:除了@Autowired方法,我们还可以通过set方法处理循环依赖问题,当然也是仅支持单例bean,多例的情况不支持

5、关闭Spring循环依赖

==================

有个疑问?Spring的循环依赖支持,默认情况是开启?是否有什么开关控制?通过源码学习,可以通过setAllowCircularReferences设置

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

context.register(AppConfiguration.class);

// 关闭Spring循环依赖支持

context.setAllowCircularReferences(false);

context.refresh();

通过测试,设置不开启这个属性的时候,即使加上@Autowired,代码还是抛异常了

6、prototype(多例)循环依赖

=======================

在多例的情况,Spring能支持循环依赖?加上@Scope(“prototype”),将bean变成多例的

Spring源码学习~循环依赖(面试必问系列!)

经过测试:多例的情况会抛出异常,即使加上了@Autowired,原因请看下文

7、Spring循环依赖特征

==================

ok,经过前面例子的验证,到这来,可以对Spring的循环依赖特点进行归纳:

  • Spring中的循环依赖场景构造器的循环依赖,通过构造函数Field属性的循环依赖,通过set方法

  • Spring的循环依赖是默认开启的(setAllowCircularReferences)

  • Spring对单例和多例Bean的支持单例Bean(singleton) :只能通过@Autowired和set方法支持多例Bean(prototype):默认不支持,直接抛异常BeanCurrentlyInCreationException

8、Spring循环依赖原理

==================

我们通过实验进行了验证,也归纳出了Spring循环依赖的特点,然后具体原因是什么?我们只能通过源码学习得到答案

在上一章的学习中,我们对Bean的创建有了一个粗略的了解,所以,顺着这条路线,跟下源码:

在前面的学习,我们知道了{@link org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean}这个方法就是Spring Bean创建的真正执行方法

protected T doGetBean(

String name, @Nullable Class requiredType, @Nullable Object[] args, boolean typeCheckOnly)

throws BeansException {

// 处理BeanName,前面说的FactoryBean带‘&’符号,要在这里进行转换

String beanName = transformedBeanName(name);

Object bean;

// Eagerly check singleton cache for manually registered singletons.

// 从map(singletonObjects)里获取单例bean,确定是否已经存在对应实例

Object sharedInstance = getSingleton(beanName);

if (sharedInstance != null && args == null) {

if (logger.isDebugEnabled()) {

if (isSingletonCurrentlyInCreation(beanName)) {

logger.debug(“Returning eagerly cached instance of singleton bean '” + beanName +

“’ that is not fully initialized yet - a consequence of a circular reference”);

}

else {

logger.debug(“Returning cached instance of singleton bean '” + beanName + “'”);

}

}

// 两种情况:普通的bean,直接从singletonObjects返回sharedInstance

//如果是FactoryBean,返回其创建的对象实例

bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);

}

else {

// Fail if we’re already creating this bean instance:

// We’re assumably within a circular reference.

// 校验是否是多例(Prototype)的Bean,多例的bean是不支持循环依赖的

// 为了避免循环依赖,遇到这种情况,直接抛出异常

if (isPrototypeCurrentlyInCreation(beanName)) {

throw new BeanCurrentlyInCreationException(beanName);

}

// Check if bean definition exists in this factory.

// 检查BeanFactory是否存在这个BeanDefinition

BeanFactory parentBeanFactory = getParentBeanFactory();

if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {

// Not found -> check parent.

// 当前容器找不到BeanDefinition,去parent容器查询

String nameToLookup = originalBeanName(name);

if (parentBeanFactory instanceof AbstractBeanFactory) {

return ((AbstractBeanFactory) parentBeanFactory).doGetBean(

nameToLookup, requiredType, args, typeCheckOnly);

}

else if (args != null) {

// Delegation to parent with explicit args.

// 返回parent容器的查询结果

return (T) parentBeanFactory.getBean(nameToLookup, args);

}

else {

// No args -> delegate to standard getBean method.

return parentBeanFactory.getBean(nameToLookup, requiredType);

}

}

if (!typeCheckOnly) {

//typeCheckOnly为false的情况,将beanName放在一个alreadyCreated的集合

markBeanAsCreated(beanName);

}

try {

RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);

checkMergedBeanDefinition(mbd, beanName, args);

// Guarantee initialization of beans that the current bean depends on.

// 校验是否配置了 depends-on

String[] dependsOn = mbd.getDependsOn();

if (dependsOn != null) {

for (String dep : dependsOn) {

// 存在循环引用的情况,要抛出异常

if (isDependent(beanName, dep)) {

throw new BeanCreationException(mbd.getResourceDescription(), beanName,

“Circular depends-on relationship between '” + beanName + “’ and '” + dep + “'”);

}

// 正常情况,注册依赖关系

registerDependentBean(dep, beanName);

try {

// 初始化被依赖项

getBean(dep);

}

catch (NoSuchBeanDefinitionException ex) {

throw new BeanCreationException(mbd.getResourceDescription(), beanName,

“'” + beanName + “’ depends on missing bean '” + dep + “'”, ex);

}

}

}

// Create bean instance.

// 单例的Bean

if (mbd.isSingleton()) {

sharedInstance = getSingleton(beanName, () -> {

try {

// 创建单例bean

return createBean(beanName, mbd, args);

}

catch (BeansException ex) {

// Explicitly remove instance from singleton cache: It might have been put there

// eagerly by the creation process, to allow for circular reference resolution.

// Also remove any beans that received a temporary reference to the bean.

destroySingleton(beanName);

throw ex;

}

});

bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);

}

// 多例的Bean,scope = protoType

else if (mbd.isPrototype()) {

// It’s a prototype -> create a new instance.

Object prototypeInstance = null;

try {

// 多例的情况,创建bean之前添加标记(用于循环依赖校验)

beforePrototypeCreation(beanName);

// 执行多例Bean创建

prototypeInstance = createBean(beanName, mbd, args);

}

finally {

// 创建原型(多例)bean之后擦除标记

afterPrototypeCreation(beanName);

}

bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);

}

// 如果不是单例bean也不是多例的bean,委托给对应的实现类

else {

String scopeName = mbd.getScope();

if (!StringUtils.hasLength(scopeName)) {

throw new IllegalStateException(“No scope name defined for bean ´” + beanName + “'”);

}

Scope scope = this.scopes.get(scopeName);

if (scope == null) {

throw new IllegalStateException(“No Scope registered for scope name '” + scopeName + “'”);

}

try {

Object scopedInstance = scope.get(beanName, () -> {

beforePrototypeCreation(beanName);

try {

// 执行bean创建

return createBean(beanName, mbd, args);

}

finally {

afterPrototypeCreation(beanName);

最后

作为过来人,小编是整理了很多进阶架构视频资料、面试文档以及PDF的学习资料,针对上面一套系统大纲小编也有对应的相关进阶架构视频资料


网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注Java)
img

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
lStateException(“No Scope registered for scope name '” + scopeName + “'”);

}

try {

Object scopedInstance = scope.get(beanName, () -> {

beforePrototypeCreation(beanName);

try {

// 执行bean创建

return createBean(beanName, mbd, args);

}

finally {

afterPrototypeCreation(beanName);

最后

作为过来人,小编是整理了很多进阶架构视频资料、面试文档以及PDF的学习资料,针对上面一套系统大纲小编也有对应的相关进阶架构视频资料

[外链图片转存中…(img-a3hRTQlE-1713281091102)]
[外链图片转存中…(img-qrpO9BDZ-1713281091102)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注Java)
[外链图片转存中…(img-WV6e9Wdb-1713281091103)]

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 14
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值