finishBeanFactoryInitialization(beanFactory);初始化剩下的全部的单实例(非懒加载)Bean(Instantiate all remaining (non-lazy-init) singletons.)缓存
1. beanFactory.preInstantiateSingletons();初始化剩下的全部的单实例(非懒加载)Beanapp
1) 首先获取全部BeanDefinition的名字List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);post
2) 根据名字获取Bean的定义信息RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);this
3) 再依次判断RootBeanDefinition不是抽象的,是单例的,不是懒加载的代理
1> 判断是否是工厂Bean,isFactoryBean(beanName),若是是的话,利用工厂来建立对象对象
2> 若是不是,就用getBean(beanName);来建立对象.接口
0. getBean(beanName)rem
1. doGetBean(name, null, null, false);get
2. 先从缓存中获取单实例Bean,若是能获取到说明这个Bean建立过(全部建立过的单实例Bean都会被缓存起来)it
Object sharedInstance = getSingleton(beanName);
/** Cache of singleton objects: bean name --> bean instance */
private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256);
3. 缓存中没有的话,就来建立对象.
4. 标记当前Bean已被建立
5. 获取Bean的定义信息
6. 获取当前Bean依赖的其它Bean(dependsOn),若是有按照getBean()把依赖的Bean先建立出来。
7. 启动单实例Bean的建立流程
1)createBean(beanName,mbd,args)
2)Object bean = resolveBeforeInstantiation(beanName, mbdToUse);让BeanPostProcessor先拦截返回代理对象
InstantiationAwareBeanPostProcessor提早执行:
先触发InstantiationAwareBeanPostProcessor接口中的postProcessBeforeInstantiation(beanClass, beanName);
若是有返回值,那么再触发InstantiationAwareBeanPostProcessor中的postProcessAfterInitialization(result, beanName);
3)若是InstantiationAwareBeanPostProcessor没有返回代理对象
4)Object beanInstance = doCreateBean(beanName, mbdToUse, args);建立Bean
1. 【建立Bean的实例】instanceWrapper = createBeanInstance(beanName, mbd, args);建立Bean
利用工厂方法或者构造器来建立Bean的实例
2. applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
调用MergedBeanDefinitionPostProcessor的bdp.postProcessMergedBeanDefinition(mbd, beanType, beanName);