/** * Internal marker for a null singleton object: * used as marker value for concurrent Maps (which don't support null values). */ protectedstaticfinal Object NULL_OBJECT = new Object();
/** Logger available to subclasses */ protectedfinal Log logger = LogFactory.getLog(getClass());
/** Cache of singleton objects: bean name --> bean instance */ // 完全实例化的组件放在这 privatefinal Map<String, Object> singletonObjects = new ConcurrentHashMap<String, Object>(256);
/** Cache of singleton factories: bean name --> ObjectFactory */ // 用于解决循环依赖的问题,组件A依赖组件B,组件B依赖组件A,那么还为完全实例的组件A将被丢入此 privatefinal Map<String, ObjectFactory<?>> singletonFactories = new HashMap<String, ObjectFactory<?>>(16);
/** Cache of early singleton objects: bean name --> bean instance */ // 用于解决循环依赖的问题,配合singletonFactories使用,这里存放的组件是还未注入属性的对象 privatefinal Map<String, Object> earlySingletonObjects = new HashMap<String, Object>(16);
/** Set of registered singletons, containing the bean names in registration order */ privatefinal Set<String> registeredSingletons = new LinkedHashSet<String>(256);
/** Names of beans that are currently in creation */ privatefinal Set<String> singletonsCurrentlyInCreation = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>(16));
/** Names of beans currently excluded from in creation checks */ privatefinal Set<String> inCreationCheckExclusions = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>(16));
/** List of suppressed Exceptions, available for associating related causes */ private Set<Exception> suppressedExceptions;
/** Flag that indicates whether we're currently within destroySingletons */ privateboolean singletonsCurrentlyInDestruction = false;
/** Disposable bean instances: bean name --> disposable instance */ privatefinal Map<String, Object> disposableBeans = new LinkedHashMap<String, Object>();
/** Map between containing bean names: bean name --> Set of bean names that the bean contains */ privatefinal Map<String, Set<String>> containedBeanMap = new ConcurrentHashMap<String, Set<String>>(16);
/** Map between dependent bean names: bean name --> Set of dependent bean names */ privatefinal Map<String, Set<String>> dependentBeanMap = new ConcurrentHashMap<String, Set<String>>(64);
/** Map between depending bean names: bean name --> Set of bean names for the bean's dependencies */ privatefinal Map<String, Set<String>> dependenciesForBeanMap = new ConcurrentHashMap<String, Set<String>>(64); //.............省略................. }
// org.springframework.beans.factory.support.DefaultListableBeanFactory protected <T> T doGetBean( final String name, final Class<T> requiredType, final Object[] args, boolean typeCheckOnly) throws BeansException { // 处理bean名称,比如beanFacotry带&,比如alias这类的别名, //逻辑简单也不是很重要,就不展开了 final String beanName = transformedBeanName(name); Object bean;
// Eagerly check singleton cache for manually registered singletons. // getSingleton这个方法经常出现,后面会展开分析 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实例,上述步骤不是获取bean实例了吗?为何还有此步骤?原因其实是考虑bean实例是beanFactory的情况,如果是beanFactory则需调用其getObject方法返回bean单例 bean = getObjectForBeanInstance(sharedInstance, name, beanName, null); }
else { // Fail if we're already creating this bean instance: // We're assumably within a circular reference. if (isPrototypeCurrentlyInCreation(beanName)) { thrownew BeanCurrentlyInCreationException(beanName); }
// Check if bean definition exists in this factory. // 父容器的情况,一般很少用到,springmvc会用到?暂不了解 BeanFactory parentBeanFactory = getParentBeanFactory(); if (parentBeanFactory != null && !containsBeanDefinition(beanName)) { // Not found -> check parent. String nameToLookup = originalBeanName(name); if (args != null) { // Delegation to parent with explicit args. return (T) parentBeanFactory.getBean(nameToLookup, args); } else { // No args -> delegate to standard getBean method. return parentBeanFactory.getBean(nameToLookup, requiredType); } }
if (!typeCheckOnly) { markBeanAsCreated(beanName); }
// Guarantee initialization of beans that the current bean depends on. // 处理depondsOn的依赖问题,如果是这种方式的循环依赖,spring也无解... String[] dependsOn = mbd.getDependsOn(); if (dependsOn != null) { for (String dep : dependsOn) { if (isDependent(beanName, dep)) { thrownew BeanCreationException(mbd.getResourceDescription(), beanName, "Circular depends-on relationship between '" + beanName + "' and '" + dep + "'"); } registerDependentBean(dep, beanName); getBean(dep); } }
// Create bean instance. if (mbd.isSingleton()) { // 这里也是个getSingleton,是另一个重载的方法,后续重点分析 sharedInstance = getSingleton(beanName, new ObjectFactory<Object>() { @Override public Object getObject()throws BeansException { try { // creatBean重点方法,后续重点分析,实例化的逻辑切入点 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); }
// Make sure bean class is actually resolved at this point, and // clone the bean definition in case of a dynamically resolved Class // which cannot be stored in the shared merged bean definition. Class<?> resolvedClass = resolveBeanClass(mbd, beanName); if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) { mbdToUse = new RootBeanDefinition(mbd); mbdToUse.setBeanClass(resolvedClass); }
// Eagerly cache singletons to be able to resolve circular references // even when triggered by lifecycle interfaces like BeanFactoryAware. // 是否允许早期单例暴露,怎么理解呢,其实就是这个组件还没完全初始化完,是否要暴露出去 // 一般都是true // 用于解决循环依赖的问题 boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences && isSingletonCurrentlyInCreation(beanName)); if (earlySingletonExposure) { if (logger.isDebugEnabled()) { logger.debug("Eagerly caching bean '" + beanName + "' to allow for resolving potential circular references"); } // 添加到SingletonFactory // 前面提过一嘴singletonFactory的问题,逻辑实现在这 addSingletonFactory(beanName, new ObjectFactory<Object>() { @Override public Object getObject()throws BeansException { // 正常情况下直接返回bean,但是要素aop增强这类的情况要特殊处理,暂不分析 return getEarlyBeanReference(beanName, mbd, bean); } }); }
// Initialize the bean instance. Object exposedObject = bean; try { // 填充属性,包括各种依赖等,我打算当个黑盒分析,逻辑很复杂,目前本人能力不够... populateBean(beanName, mbd, instanceWrapper); if (exposedObject != null) { // 初始化bean,beanPostProcessor接口的切人点就在这,还有InitializingBean接口的切入点也在这 // 逻辑比较简单,总而言之是支持对实例化后的bean做个性化的改造 exposedObject = initializeBean(beanName, exposedObject, mbd); } } catch (Throwable ex) { if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) { throw (BeanCreationException) ex; } else { thrownew BeanCreationException( mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex); } } // 这部分暂不分析,不理解,太菜了! if (earlySingletonExposure) { Object earlySingletonReference = getSingleton(beanName, false); if (earlySingletonReference != null) { if (exposedObject == bean) { exposedObject = earlySingletonReference; } elseif (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) { String[] dependentBeans = getDependentBeans(beanName); Set<String> actualDependentBeans = new LinkedHashSet<String>(dependentBeans.length); for (String dependentBean : dependentBeans) { if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) { actualDependentBeans.add(dependentBean); } } if (!actualDependentBeans.isEmpty()) { thrownew BeanCurrentlyInCreationException(beanName, "Bean with name '" + beanName + "' has been injected into other beans [" + StringUtils.collectionToCommaDelimitedString(actualDependentBeans) + "] in its raw version as part of a circular reference, but has eventually been " + "wrapped. This means that said other beans do not use the final version of the " + "bean. This is often the result of over-eager type matching - consider using " + "'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example."); } } } }