本篇内容主要介绍SpringApplication启动过程中上下文刷新的第十一步finishBeanFactoryInitialization中最关键的创建单例Bean的步骤.它也包含了部分的Bean生命周期钩子调用.
AbstractAutowireCapableBeanFactory.doCreateBean
protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) throws BeanCreationException { // 存放当前Bean的对象的包装类 BeanWrapper instanceWrapper = null; // 如果该Bean是单例的,先从缓存中删掉该Bean.防止有重复 if (mbd.isSingleton()) { instanceWrapper = this.factoryBeanInstanceCache.remove(beanName); } // 如果没有创建过,这里会先创建一个包含Bean的包装类实例 if (instanceWrapper == null) { instanceWrapper = createBeanInstance(beanName, mbd, args); } // 这个就是Bean实例 Object bean = instanceWrapper.getWrappedInstance(); Class<?> beanType = instanceWrapper.getWrappedClass(); // 给BeanDefinition确定该Bean的类型 if (beanType != NullBean.class) { mbd.resolvedTargetType = beanType; } // Allow post-processors to modify the merged bean definition. synchronized (mbd.postProcessingLock) { if (!mbd.postProcessed) { try { // 可以理解为Bean定义合并为RootBeanDefinition的后置处理器 // 执行实现MergedBeanDefinitionPostProcessor接口postProcessMergedBeanDefinition方法 // 主要是处理@PostConstruct,@Autowire,@Value,@Resource,@PreDestory等这些注解 // 注解处理完加入BeanDefinition中为后续初始化调用准备 // AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、 // InitDestroyAnnotationBeanPostProcessor、ScheduledAnnotationBeanPostProcessor、 // ApplicationListenerDetector看这些实现类名字就可以了解作用了 applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName); } catch (Throwable ex) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Post-processing of merged bean definition failed", ex); } mbd.postProcessed = true; } } // 判断该Bean是否是单例,是否支持循环依赖,当前是否正在被创建 // 如果符合早期单例Bean被暴露的条件,将它放到三级缓存中暴露出去,防止循环依赖. boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences && isSingletonCurrentlyInCreation(beanName)); if (earlySingletonExposure) { if (logger.isTraceEnabled()) { logger.trace("Eagerly caching bean '" + beanName + "' to allow for resolving potential circular references"); } // 添加至三级缓存中,会执行SmartInstantiationAwareBeanPostProcessor#getEarlyBeanReference方法去寻找到前期的Bean们 addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean)); } // 这个就是要初始化并且用以后面使用的Bean Object exposedObject = bean; try { // 对bean进行填充: 给已经初始化的属性们赋值,在这里面完成依赖注入的相关内容 // 这里会执行postProcessAfterInstantiation判断是否需要赋值 // 如果需要则会执行postProcessProperties. populateBean(beanName, mbd, instanceWrapper); // 属性填充完,则执行该Bean初始化的实现流程 // 若实现了BeanNameAware, BeanClassLoaderAware,BeanFactoryAwareAware接口,则注入相关对象 // 遍历后置处理器,调用实现的postProcessBeforeInitialization方法 // 如果实现了initialzingBean,调用实现的 afterPropertiesSet(),如果配置了init-mothod,调用相应的init方法 // 遍历后置处理器,调用实现的postProcessAfterInitialization // 这里没啥好说的,调用上面接口的实现方法回调.可看我的github上spring-learn. exposedObject = initializeBean(beanName, exposedObject, mbd); } catch (Throwable ex) { if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) { throw (BeanCreationException) ex; } else { throw new BeanCreationException( mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex); } } // 如果该Bean支持循环依赖,从缓存中获取Bean对象(从三级缓存放入二级缓存). if (earlySingletonExposure) { Object earlySingletonReference = getSingleton(beanName, false); // 检查是否和之前放入缓存中一致.不一致则需要抛出异常--之前依赖注入的该Bean非最终版本 if (earlySingletonReference != null) { if (exposedObject == bean) { exposedObject = earlySingletonReference; } else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) { String[] dependentBeans = getDependentBeans(beanName); Set<String> actualDependentBeans = new LinkedHashSet<>(dependentBeans.length); for (String dependentBean : dependentBeans) { if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) { actualDependentBeans.add(dependentBean); } } if (!actualDependentBeans.isEmpty()) { throw new 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 " + "'getBeanNamesForType' with the 'allowEagerInit' flag turned off, for example."); } } } } // Register bean as disposable. try { // 注册到需要执行销毁的Bean集合中 // 其中需要实现 DisposableBean 或 AutoCloseable接口并且是单例 // 会执行他们的destory或close方法 // 但在之前会执行DestructionAwareBeanPostProcessor的postProcessBeforeDestruction方法 registerDisposableBeanIfNecessary(beanName, bean, mbd); } catch (BeanDefinitionValidationException ex) { throw new BeanCreationException( mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex); } return exposedObject; }
AbstractAutowireCapableBeanFactory.createBeanInstance
protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) { // 确保类加载解析过 Class<?> beanClass = resolveBeanClass(mbd, beanName); // 创建的Bean必须是public的 if (beanClass != null && !Modifier.isPublic(beanClass.getModifiers()) && !mbd.isNonPublicAccessAllowed()) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Bean class isn't public, and non-public access not allowed: " + beanClass.getName()); } // 如果有Supplier回调对象,则通过该对象回调方法创建实例 Supplier<?> instanceSupplier = mbd.getInstanceSupplier(); if (instanceSupplier != null) { return obtainFromSupplier(instanceSupplier, beanName); } // 使用工厂方法来进行bean的实例化 if (mbd.getFactoryMethodName() != null) { return instantiateUsingFactoryMethod(beanName, mbd, args); } // 这里的操作当重复创建Bean时会比较快速.有已解析过标志位 // 在使用构造器创建实例后,Spring会将解析过后确定下来的构造器或工厂方法保存在缓存中,避免再次创建相同bean时再次解析 // 然后根据参数不同调用指定的构造器 boolean resolved = false; boolean autowireNecessary = false; if (args == null) { synchronized (mbd.constructorArgumentLock) { if (mbd.resolvedConstructorOrFactoryMethod != null) { resolved = true; autowireNecessary = mbd.constructorArgumentsResolved; } } } // 如果已经解析过并且是需要装配的,则调用autowireConstructor.否则调用默认构造器创建 if (resolved) { if (autowireNecessary) { return autowireConstructor(beanName, mbd, null, null); } else { return instantiateBean(beanName, mbd); } } // 没有解析过则先去解析获取该类的构造函数.再然后还是判断是用默认构造器构件还是交给Spring自动装配 // determineConstructorsFromBeanPostProcessors将自动扫描通过@Autowired/@Value注解的构造器从而可以完成构造器注入 Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName); if (ctors != null || mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR || mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) { return autowireConstructor(beanName, mbd, ctors, args); } // Preferred constructors for default construction? ctors = mbd.getPreferredConstructors(); if (ctors != null) { return autowireConstructor(beanName, mbd, ctors, null); } // No special handling: simply use no-arg constructor. return instantiateBean(beanName, mbd); }
参考
AbstractBeanFactory#getBean()、doGetBean完成Bean的初始化、实例化,以及BeanPostProcessor后置处理器源码级详细分析