示例:AService依赖BService; BService依赖AService缓存
@Service public class AService { // @Autowired public BService bService; }
@Service public class BService { @Autowired public AService aService; }
上面演示的例子就是循环注入app
若是改成多例的化,运行时就会报错,循环引用异常,找不到对象函数
@Scope("prototype")
Spring中的循环依赖问题在单例的状况下,Spring是已经帮咱们解决好了,多例没有解决循环依赖问题。源码分析
为啥,多例的状况下 Spring没有去解决循环依赖问题?this
由于在多例的状况下,设置的多例的对象没有明确哪个,就会产生循环依赖问题。spa
咱们能够本身去:明确指定引用那个对象prototype
@Service @Scope("prototype") public class AService { // @Autowired public BService bService; // 为何Aservice在建立的时候 为何Bservice比ASERVICE 先建立 public AService() { System.out.println("AService被Java的反射技术建立"); } public void setbService(BService bService) { this.bService = bService; } }
@Service @Scope("prototype") public class BService { // @Autowired public AService aService; public void setaService(AService aService) { this.aService = aService; } }
public class SpringApp { public static void main(String[] args) { // 1. ioc容器在建立的时候全部的单例对象是否是会被建立 AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig.class); // // 对例子状况 当你在调用的时候才获取 AService aSerivce = applicationContext.getBean("AService", AService.class); BService bSerivce = applicationContext.getBean("BService", BService.class); aSerivce.setbService(bSerivce); bSerivce.setaService(aSerivce); // 循环引用异常 找不到对象 /** * 思考问题? 若是咱们的项目对象必需要是多例? 并且必需要循环引用 明确的指定引用那个对象 */ String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames(); for (int i = 0; i < beanDefinitionNames.length; i++) { System.out.println(beanDefinitionNames[i]); }
思考问题:单例对象在何时建立?debug
在IOC容器被建立的时候建立3d
多例的状况下,是在getbean()调用的状况下建立。多例对象每次用完就会去销毁掉。对象
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig.class);
// Instantiate all remaining (non-lazy-init) singletons. finishBeanFactoryInitialization(beanFactory);
// Instantiate all remaining (non-lazy-init) singletons. beanFactory.preInstantiateSingletons();
public Object getBean(String name) throws BeansException { return this.doGetBean(name, (Class)null, (Object[])null, false); }
public Object getSingleton(String beanName) { return this.getSingleton(beanName, true); }
获取缓存对象:
protected Object getSingleton(String beanName, boolean allowEarlyReference) { Object singletonObject = this.singletonObjects.get(beanName); //根据BeanName去集合中查,查到就返回这个对象,【】【】【】一级缓存对象集合【】【】【】缓存完整对象【】【】完整对象表示对象已经建立完了,而且对象属性已经赋值了。 if (singletonObject == null && this.isSingletonCurrentlyInCreation(beanName)) { Map var4 = this.singletonObjects; synchronized(this.singletonObjects) { singletonObject = this.earlySingletonObjects.get(beanName); //查询二级缓存中是否有缓存对象 if (singletonObject == null && allowEarlyReference) { ObjectFactory<?> singletonFactory = (ObjectFactory)this.singletonFactories.get(beanName); //查询三级缓存,三级缓存中有的化,将三级缓存中的数据放入二级缓存中 if (singletonFactory != null) { singletonObject = singletonFactory.getObject(); this.earlySingletonObjects.put(beanName, singletonObject); this.singletonFactories.remove(beanName); } } } } return singletonObject; }
一级缓存没有找到,就去找二级缓存中找
singletonObject == null && this.isSingletonCurrentlyInCreation(beanName) //一级缓存没有,而且singletonsCurrentlyInCreation判断是否以前标记为该对象开始建立
if (isPrototypeCurrentlyInCreation(beanName)) { //咱们对象是单例的,全部不进入 throw new BeanCurrentlyInCreationException(beanName); }
public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) { Assert.notNull(beanName, "Bean name must not be null"); synchronized (this.singletonObjects) { Object singletonObject = this.singletonObjects.get(beanName); if (singletonObject == null) { if (this.singletonsCurrentlyInDestruction) { throw new BeanCreationNotAllowedException(beanName, "Singleton bean creation not allowed while singletons of this factory are in destruction " + "(Do not request a bean from a BeanFactory in a destroy method implementation!)"); } if (logger.isDebugEnabled()) { logger.debug("Creating shared instance of singleton bean '" + beanName + "'"); } beforeSingletonCreation(beanName); boolean newSingleton = false; boolean recordSuppressedExceptions = (this.suppressedExceptions == null); if (recordSuppressedExceptions) { this.suppressedExceptions = new LinkedHashSet<>(); } try { singletonObject = singletonFactory.getObject(); newSingleton = true; } catch (IllegalStateException ex) { // Has the singleton object implicitly appeared in the meantime -> // if yes, proceed with it since the exception indicates that state. singletonObject = this.singletonObjects.get(beanName); if (singletonObject == null) { throw ex; } } catch (BeanCreationException ex) { if (recordSuppressedExceptions) { for (Exception suppressedException : this.suppressedExceptions) { ex.addRelatedCause(suppressedException); } } throw ex; } finally { if (recordSuppressedExceptions) { this.suppressedExceptions = null; } afterSingletonCreation(beanName); } if (newSingleton) { addSingleton(beanName, singletonObject); } } return singletonObject; } }
protected void beforeSingletonCreation(String beanName) { if (!this.inCreationCheckExclusions.contains(beanName) && !this.singletonsCurrentlyInCreation.add(beanName)) { throw new BeanCurrentlyInCreationException(beanName); } }
标识为该对象开始建立
最终调用
正真去建立咱们的Bean对象:
既然要建立对象,先反射走无参构造函数,对象先实例化完成,在赋值
执行这个方法,输出构造函数打印的语句,说明底层经过Java反射机制初始化的
在这以前,咱们的对象属于婴儿对象,由于它的属性尚未赋值。都是称为婴儿对象。
那么何时赋值呢?
populateBean(beanName, mbd, instanceWrapper);//这里给对象属性赋值
在给对象属性赋值以前:
protected void addSingletonFactory(String beanName, ObjectFactory<?> singletonFactory) { Assert.notNull(singletonFactory, "Singleton factory must not be null"); synchronized (this.singletonObjects) { if (!this.singletonObjects.containsKey(beanName)) { //【】【】若是一级缓存没有该对象的状况下,会将该对象存放在三级缓存中 this.singletonFactories.put(beanName, singletonFactory); //【】【】存放在三级缓存中,对象实例化完成,可是没有赋值,婴儿对象【】【】 this.earlySingletonObjects.remove(beanName); this.registeredSingletons.add(beanName); } } }
A对象坚持依赖B对象,这时候B对象也须要被建立
A对象已经存放在三级缓存中,这时候要去建立B对象
此时B对象也要走A对象流程
看下调用链
也将B对象放入三级缓存
总结下:
AService在建立的时候,提早曝光存放到三级缓存中,AService发现依赖BService,这时候Bservice提早曝光存放到三级缓存中去。
此时BService又依赖AService,此时BService通过赋值是完整对象,可是Aservice仍是婴儿对象,没有彻底建立完毕。
就会去把BService对象注册到一级缓存中,同时会把以前缓存BService对象的二级缓存清除掉
AService对象依赖BService,BService此时已经建立成功了,那么AService在设置属性后,就直接把BService赋值给AService。
开始注册AService对象
SpringBean中 Aservic对象被建立流程步骤源码分析:
- doGetBean建立咱们bean对象
- getSingleton (beanName) 获取缓存对象
注意:完整对象概念:对象已经实例化成功而且全部属性都已经赋值
- singletonObjects 一级缓存完整对象
- earlySingletonObjects 二级缓存 缓存婴儿对象
- singletonFactories 三级缓存存放婴儿对象
理解概念:
- 完整对象表示该对象实例化完成而且全部的属性已经赋值。
- 婴儿对象(提早对象)对象已经实例化完成可是属性没有赋值的。
singletonObject ==null&&this.singletonsCurrentlyInCreation.contains(beanName);
{
才可以查询二级缓存
}
singletonsCurrentlyInCreation :标记为该对象开始建立
- getSingleton(String beanName, ObjectFactory<?> singletonFactory)
this.singletonsCurrentlyInCreation.add(beanName) 表示该对象已经开始建立
- createBean() →doCreateBean
- addSingletonFactory 将婴儿对象(不完整对象也就是只是实例化完成可是属性没有赋值的) 存放三级缓存中。
- A对象已经存放到三级缓存中,开始给对象属性赋值的时候 须要建立B对象。
A对象检查发现依赖B对象 这时候B对象也须要被建立
本文参考:
蚂蚁课堂