5.4获取单例
DefaultSingletonBeanRegistry public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) { Assert.notNull(beanName, "Bean name must not be null"); //全局变量需要 synchronized (this.singletonObjects) { //首先检查对应的bean是否已经加载过了,因为singleton模式其实就是复用创建的bean //所以这一步是必须的 Object singletonObject = this.singletonObjects.get(beanName); //缓存中没有进行singleton的bean的初始化 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 + "'"); } //通过singletonsCurrentlyInCreation.add记录加载状态,对循环依赖进行检测 beforeSingletonCreation(beanName); boolean newSingleton = false; boolean recordSuppressedExceptions = (this.suppressedExceptions == null); if (recordSuppressedExceptions) { this.suppressedExceptions = new LinkedHashSet<>(); } try { //初始化bean //啊...ObjectFactory<?> singletonFactory,你从哪里来,你是怎么来的 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; } //移除缓存中对该bean的正在加载的状态 afterSingletonCreation(beanName); } //加入缓存 if (newSingleton) { addSingleton(beanName, singletonObject); } } return singletonObject; } }
大致逻辑:
1.检查缓存是否已经加载过
2.若没有加载,则记录beanName的正在加载状态
3.加载单例前记录加载状态(重要的地方,这里用来判断循环依赖)
protected void beforeSingletonCreation(String beanName) { //记录加载状态,也就是通过this.singletonsCurrentlyInCreation.add将当前正要创建的bean记录在缓存中,可以对循环依赖进行检测 if (!this.inCreationCheckExclusions.contains(beanName) && !this.singletonsCurrentlyInCreation.add(beanName)) { throw new BeanCurrentlyInCreationException(beanName); } }
4.通过调用参数传入的ObjectFactory实例化bean(详情在5.7创建bean)
5.加载单例后的处理方法调用
protected void afterSingletonCreation(String beanName) { //移除缓存中对该bean的正在加载的状态 if (!this.inCreationCheckExclusions.contains(beanName) && !this.singletonsCurrentlyInCreation.remove(beanName)) { throw new IllegalStateException("Singleton '" + beanName + "' isn't currently in creation"); } }
6.将结果记录至缓存并删除加载bean过程中所记录的各种辅助状态
protected void addSingleton(String beanName, Object singletonObject) { synchronized (this.singletonObjects) { this.singletonObjects.put(beanName, singletonObject); this.singletonFactories.remove(beanName); this.earlySingletonObjects.remove(beanName); this.registeredSingletons.add(beanName); } }
但是,第四步中ObjectFactory是从哪里来,怎么创建的呢。5.5准备创建bean说明。

浙公网安备 33010602011771号