其中包括各个主要类之间的联系,以及对实现Bean解析的Refresh()、createBean()、getBean()方法的剖析。
@Override public void refresh() throws BeansException, IllegalStateException { synchronized (this.startupShutdownMonitor) { // Prepare this context for refreshing. //准备所有需要初始化的资源 // 1. 设定开始时间 // 2. 初始化配置文件 -- 空实现,默认不作处理 // 3. 添加环境中没有的配置 // 4. 保存应用监听器 // 5. 初始化earlyApplicationEvents,在multicaster可用时发布事件 prepareRefresh(); // Tell the subclass to refresh the internal bean factory. // 1. 销毁所有单例Beans,关闭当前工厂 // 2. 新建DefaultListableBeanFactory工厂并设置 // 3. loadBeanDefinitions方法加载BeanDefinitions到工厂 ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); // Prepare the bean factory for use in this context. // 1. 让工厂使用环境类的类加载器,添加Spring EL解析器,添加配置编辑器注册商(其为PropertyEditor注册器数组,它的职责是用来注册PropertyEditor,而PropertyEditor可以定义如何解析配置,比如将2019-08-18解析成java.util.date.class) // 2. 添加ApplicationContextAwareProcessor到工厂,主要用于在bean创建时,实例化后,初始化前,检查bean上是否实现了某个Aware接口,有的话做相应调用。 // 3. 忽略EnvironmentAware、EmbeddedValueResolverAware、ResourceLoaderAware、ApplicationEventPublisherAware、MessageSourceAware、ApplicationContextAware接口自动注入的依赖。这里解释一下为什么要忽略这些注入:Spring自动创建Bean,但Bean是无状态的,也就是说Bean不知道Spring容器BeanFactory的任何信息,包括Bean自己的名称name,Spring这样做的目的是为了Spring容器和Bean的解耦。那么如果Bean要想定制化的做一些操作,就必须要获取BeanFactory中的信息,在Spring Bean的生命周期中我们都知道实现一些列接口去观察Bean创建过程中的一些信息。所以我们在创建Bean的过程中通过实现一些接口的方式去获取BeanDefinition信息。但这里我们需要正常实例化一个Bean并不需要去拿到这些信息,所以忽略掉这些接口。 // 4. 建立一些没有注册到工厂中的自动装配Bean映射关联关系(BeanFactory、ResourceLoader、ApplicationEventPublisher、ApplicationContext) 即:有些对象并不在BeanFactory中,但是我们依然想让它们可以被装配。 // 5. 添加ApplicationListenerDetector到工厂,用于探测是应用监听器的内部Bean // 6. 判断是否包含了类加载时期的代码织入(AspectJ)。包含则添加LoadTimeWeaverAwareProcessor(用于给LoadTimeWeaverAware Bean添加loadTimeWeaver类)到工厂 // 7. 不存在则注册环境、系统配置、系统环境Bean到工厂 prepareBeanFactory(beanFactory); try { // Allows post-processing of the bean factory in context subclasses. // 允许子类在所有的bean尚未初始化之前注册BeanPostProcessor,空实现且没有子类覆盖。 postProcessBeanFactory(beanFactory); // Invoke factory processors registered as beans in the context. // 允许我们在bean正式初始化之前改变其值。有两种方式: // 通过代码的方式:context.addBeanFactoryPostProcessor // 通过xml配置的方式:<bean class="base.SimpleBeanFactoryPostProcessor" /> // 具体怎么实现的这里不赘诉了,主要源码在PostProcessorRegistrationDelegate中,通过使用BeanFactory的getBeanNamesForType方法获取相应的BeanDefinition的name数组,之后逐一调用getBean方法获取到bean(初始化),这里有一个优先级的概念,如果你的BeanFactoryPostProcessor同时实现了Ordered或者是PriorityOrdered接口,那么会被首先执行。下面registerBeanPostProcessors也是一样的 invokeBeanFactoryPostProcessors(beanFactory); // Register bean processors that intercept bean creation. // 简述一下就是在BeanDefinitions中寻找BeanPostProcessor,之后调用BeanFactory.addBeanPostProcessor方法保存在一个List中,添加的时侯有优先级的概念,优先级高的在前面。 registerBeanPostProcessors(beanFactory); // Initialize message source for this context. // 初始化国际化资源 // 1. 如果工厂包含messageSource Bean,就调用getBean方法完成其初始化并将其保存在AbstractApplicationContext内部messageSource成员变量中,用来处理getMessage调用。此处使用了委托模式。 // 2. 如果没有配置此bean,那么初始化一个DelegatingMessageSource对象,使用一个空实现用以处理getMessage调用请求。 initMessageSource(); // Initialize event multicaster for this context. // 1. 在BeanFactory中寻找ApplicationEventMulticaster的bean,如果找到,那么调用getBean方法将其初始化,同样这里将applicationEventMulticaster保存在变量中,并使用委托模式来处理调用publishEvent时,对委托对象的multicastEvent方法的间接调用。 // 2. 如果找不到那么使用SimpleApplicationEventMulticaster。 initApplicationEventMulticaster(); // Initialize other special beans in specific context subclasses. // 模版方法,允许子类在进行bean初始化之前进行一些定制操作。默认空实现。 onRefresh(); // Check for listener beans and register them. // 1. 添加当前AbstractAppContext环境的应用监听器到ApplicationEventMulticaster中 // 2. 存储所有Bean名称到AbstractApplicationEventMulticaster内部类istenerRetriever.applicationListenerBeans属性中 // 3. 发布early application events,如果不为空 registerListeners(); // Instantiate all remaining (non-lazy-init) singletons. // 1. 如果包含conversionService Bean那么就设置ConversionService(其作用和PropertyEditor一致都是转换用)到工厂 // 2. 如果不包含embeddedValueResolver(用于解析注解的值),设置默认值 // 3. 初始化LoadTimeWeaverAware Bean,用以能完成转译类 // 4. 停止使用临时类加载器,冻结且缓存工厂配置不允许修改, // 5. 初始化所有预加载的单例Bean // 1. 通过beanDefinitionNames遍历beans // 2. 如果当前遍历的bean不是抽象、不是懒加载、是单例的那么就判断当前bean是否是FactoryBean,如果是就给每个bean加上前缀“&”,并通过getBean初始化bean,如果此bean是FactoryBean类型还是SmartFactoryBean类型,那么需要判断是否需要eagerInit马上进行初始化。 // 3. 如果当前bean不是FactoryBean,就直接调用getBean进行初始化 // 4. 待遍历所有bean初始化完成后还需要遍历所有是SmartInitializingSingleton类型的bean来触发该类型接口的afterSingletonsInstantiated方法。(这里的应用场景主要是在所有单例 bean 创建完成之后,可以在该回调中做一些事情。) finishBeanFactoryInitialization(beanFactory); // Last step: publish corresponding event. // 1. 清理资源缓存 // 2. 如果存在初始化生命周期处理器,则创建lifecycleProcessor bean,否则创建默认的DefaultLifecycleProcessor bean // 3. 委托给LifecycleProcessor去调用所有实现了SmartLifecycle bean的start方法 (这里SmartLifecycle的使用场景主要是在Spring加载和初始化所有bean后,接着执行一些任务或者启动需要的异步服务) // 4. 发布最后的消息 // 5. 将环境注册到LiveBeansView:如果存在spring.liveBeansView.mbeanDomain配置则注册LiveBeansView到MBeanServer中(其实现了LiveBeansViewMBean接口的getSnapshotAsJson,主要用于将环境转换为json) finishRefresh(); } catch (BeansException ex) { if (logger.isWarnEnabled()) { logger.warn("Exception encountered during context initialization - " + "cancelling refresh attempt: " + ex); } // Destroy already created singletons to avoid dangling resources. destroyBeans(); // Reset 'active' flag. cancelRefresh(ex); // Propagate exception to caller. throw ex; } finally { // Reset common introspection caches in Spring's core, since we // might not ever need metadata for singleton beans anymore... resetCommonCaches(); } } } 复制代码
// 先说一下createBean // 经过resolveBeforeInstantiation方法后,如果创建了代理(即重写了InstantiationAwareBeanPostProcessor的postProcessBeforeInstantiation方法并且在方法中改变了bean)直接返回即可,否则进行常规bean的创建 @Override AbstractAutowireCapableBeanFactory.createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args){ } // 常规执行创建bean的逻辑 // 这里BeanWrapper相当于一个代理器,Spring委托BeanWrapper完成Bean属性的填充工作。在Bean实例被InstantiatioonStrategy创建出来之后,容器主控程序将Bean实例通过BeanWrapper包装起来 protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args){ //Instantiate the bean BeanWrapper instanceWrapper = null; if(mbd.isSingleton()){ // 清理缓存中的bean instanceWrapper = this.factoryBeanInstanceCache.remove(beanName); } if(instanceWrapper == null){ // 使用对应的策略创建新的实例:工厂方法,构造函数自动注入,简单初始化 instanceWrapper = createBeanInstance(beanName,mbd,args); } final Object bean = instanceWrapper.getWrappedInstance(); Class<?> beanType = instanceWrapper.getWrappedClass(); if (beanType != NullBean.class) { mbd.resolvedTargetType = beanType; } // Allow post-processors to modify the merged bean definition. synchronized (mbd.postProcessingLock) { if (!mbd.postProcessed) { try { // MergedBeanDefinitionPostProcessor接口是在合并处理Bean定义的时候的回调 // 这儿方法是将MergedBeanDefinitionPostProcessor应用到这个特定的bean定义上 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正在创建 boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences && isSingletonCurrentlyInCreation(beanName)); if(earlySingletonExposure){ addSingletonFactory(beanName, new ObjectFactory(){public Object getObject() throws BeansException{ //对bean再一次依赖引用,主要引用SmartInstantiationAwareBeanPostProcessor, //AOP就是在这里将advice动态织入bean中,若没有直接返回bean. return getEarlyBeanReference(beanName,mbd,bean); }}); } // Initialize the bean instance. Object exposedObject = bean; try { //对bean进行填充,将各个属性值注入,可能存在依赖于其他bean的属性,递归依赖bean populateBean(beanName, mbd, instanceWrapper); //调用初始化方法 如 init-method 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); } } if(earlySingletonExposure){ Object earlySingletonReference = getSingleton(beanName,false); // 如果有循环依赖 if(earlySingletonReference != null){ //没有被代理增强 if(exposedObject == bean){ exposedObject = earlySingletonReference; }else if(!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); } } //如果actualDependentBeans不为空表示当前bean创建后其依赖的bean却没有创建完,说明存在循环依赖 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 " + "'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example."); } } } try{ //注册为一次性可销毁的bean registerDisposableBeanIfNecessary(beanName,bean,mbd); }catch (BeanDefinitionValidationException ex) { throw new BeanCreationException( mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex); } return exposedObject; } 复制代码
// 重点解析Bean的初始化 -- getBean(name) 为例 @Override public Object getBean(String name) throws BeansException { return doGetBean(name, null, null, false); } // 四个参数:第一个为传入的beanName,第二个为bean的Class类型,第三个表示创建bean需要的参数,最后一个表示不需要进行类型检查。 protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType, @Nullable final Object[] args, boolean typeCheckOnly) throws BeansException { // 将是factoryBean的名称去掉 之前加的 “&” 符号 final String beanName = transformedBeanName(name); Object bean; // 1. 检测是不是Spring手动注册的那些单例bean // 2. 如果是,那么再检测是不是工厂bean,如果是返回其工厂方法返回的实例,如果不是返回bean本身。 // Eagerly check singleton cache for manually registered singletons. Object sharedInstance = getSingleton(beanName); if (sharedInstance != null && args == null) { if (logger.isTraceEnabled()) { if (isSingletonCurrentlyInCreation(beanName)) { logger.trace("Returning eagerly cached instance of singleton bean '" + beanName + "' that is not fully initialized yet - a consequence of a circular reference"); } else { logger.trace("Returning cached instance of singleton bean '" + beanName + "'"); } } 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)) { throw new BeanCurrentlyInCreationException(beanName); } // 如果父容器存在,并且存在此bean定义,那么交由其父容器初始化bean // Check if bean definition exists in this factory. BeanFactory parentBeanFactory = getParentBeanFactory(); if (parentBeanFactory != null && !containsBeanDefinition(beanName)) { // Not found -> check parent. // 将是factoryBean的name前加上 “&” ,因为在父容器中又会再次进行这个转换操作,所以这里还需要加上 & 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. return (T) parentBeanFactory.getBean(nameToLookup, args); } else if (requiredType != null) { // No args -> delegate to standard getBean method. return parentBeanFactory.getBean(nameToLookup, requiredType); } else { return (T) parentBeanFactory.getBean(nameToLookup); } } // 这里使用了synchronized的双重校验来将这个beanName加入到alreadyCreated中,用以防止有其他进程在同一时间创建 if (!typeCheckOnly) { markBeanAsCreated(beanName); } try { // 获取到具有父子关系的bean定义的根节点 final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName); checkMergedBeanDefinition(mbd, beanName, args); // 拿到这个bean所依赖的所有bean定义根节点,并递归调用初始化它们 // Guarantee initialization of beans that the current bean depends on. String[] dependsOn = mbd.getDependsOn(); if (dependsOn != null) { for (String dep : dependsOn) { // registerDependentBean进行了依赖关系的注册,这么做的原因是Spring在即进行bean销毁的时候会首先销毁被依赖的bean。依赖关系的保存是通过一个ConcurrentHashMap<String, Set>完成的,key是bean名字。 if (isDependent(beanName, dep)) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Circular depends-on relationship between '" + beanName + "' and '" + dep + "'"); } registerDependentBean(dep, beanName); try { // getBean方法是包括了所有scope的初始化的 getBean(dep); } catch (NoSuchBeanDefinitionException ex) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "'" + beanName + "' depends on missing bean '" + dep + "'", ex); } } } // Create bean instance. if (mbd.isSingleton()) { // // 1. getSingleton首先会尝试去获取这个Bean // 2. 然后判断是否为空,是否正在创建中,如果是则跳过,直接返回获取到的Bean // 3. 如果都没有,则尝试createBean去创建Bean // 4. 然后就是一些收尾工作,把Bean加到singletonObjects中 sharedInstance = getSingleton(beanName, () -> { try { 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); } else if (mbd.isPrototype()) { // It's a prototype -> create a new instance. Object prototypeInstance = null; try { // 确保在同一时刻只有一个Bean在初始化 beforePrototypeCreation(beanName); prototypeInstance = createBean(beanName, mbd, args); } finally { // 标记此bean已经没有处于实例化时段了 afterPrototypeCreation(beanName); } bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd); } // 其余诸如:request、session这样的Scope的初始化,这里就和前面的单例初始化比较雷同,不讲了 else { String scopeName = mbd.getScope(); final 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 { return createBean(beanName, mbd, args); } finally { afterPrototypeCreation(beanName); } }); bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd); } catch (IllegalStateException ex) { throw new BeanCreationException(beanName, "Scope '" + scopeName + "' is not active for the current thread; consider " + "defining a scoped proxy for this bean if you intend to refer to it from a singleton", ex); } } } catch (BeansException ex) { cleanupAfterBeanCreationFailure(beanName); throw ex; } } // Check if required type matches the type of the actual bean instance. if (requiredType != null && !requiredType.isInstance(bean)) { try { T convertedBean = getTypeConverter().convertIfNecessary(bean, requiredType); if (convertedBean == null) { throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass()); } return convertedBean; } catch (TypeMismatchException ex) { if (logger.isTraceEnabled()) { logger.trace("Failed to convert bean '" + name + "' to required type '" + ClassUtils.getQualifiedName(requiredType) + "'", ex); } throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass()); } } return (T) bean; } 复制代码