refresh 方法,至此,BeanFactory 初始化已完成。
一、registerBeanPostProcessors
注册 BeanPostProcessor,bean 的后置处理器。
二、initMessageSource
初始化消息源、国际化等,不重要。
三、initApplicationEventMulticaster
初始化事件广播,不重要。
四、onRefresh
目前没有任何实现,供子类扩展。
五、registerListeners
检查监听器并注册。
六、finishBeanFactoryInitialization
完成 BeanFactory 中所有非延迟加载的 bean 的实例化。
1 2 3 4
| beanFactory.preInstantiateSingletons();
org.springframework.beans.factory.support.DefaultListableBeanFactory#preInstantiateSingletons
|
preInstantiateSingletons 方法
实例化单例 bean。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);
for (String beanName : beanNames) { RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName); if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) { if (isFactoryBean(beanName)) { Object bean = getBean(FACTORY_BEAN_PREFIX + beanName); if (bean instanceof FactoryBean) { FactoryBean<?> factory = (FactoryBean<?>) bean; boolean isEagerInit; if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) { isEagerInit = AccessController.doPrivileged( (PrivilegedAction<Boolean>) ((SmartFactoryBean<?>) factory)::isEagerInit, getAccessControlContext()); } else { isEagerInit = (factory instanceof SmartFactoryBean && ((SmartFactoryBean<?>) factory).isEagerInit()); } if (isEagerInit) { getBean(beanName); } } } else { getBean(beanName); } } }
|
doGetBean 方法
判断是否为 FactoryBean
,如果是,则进行转换并得到 beanName。
1 2 3
| String beanName = transformedBeanName(name); Object bean;
|
检查单例缓存中是否有手动添加的实例,这个方法在初始化的时候会调用,在 getBean
的时候也会调用,即,在 Spring 初始化的时候会先获取一下这个单例的实例,判断这个对象是否已经被实例化好了,大多数情况下该对象绝对为空,但懒加载的情况下,需要先获取一次。
1
| Object sharedInstance = getSingleton(beanName);
|
alreadyCreated 存储已被创建的 bean,解决循环依赖。
1 2 3 4
| if (!typeCheckOnly) { markBeanAsCreated(beanName); }
|
1 2 3 4
| RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName); checkMergedBeanDefinition(mbd, beanName, args);
String[] dependsOn = mbd.getDependsOn();
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| if (mbd.isSingleton()) { sharedInstance = getSingleton(beanName, () -> { try { return createBean(beanName, mbd, args); } catch (BeansException ex) { destroySingleton(beanName); throw ex; } }); bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd); }
|
createBean 方法
1 2
| Object beanInstance = doCreateBean(beanName, mbdToUse, args);
|
doCreateBean 方法
1 2 3 4
| if (instanceWrapper == null) { instanceWrapper = createBeanInstance(beanName, mbd, args); }
|
createBeanInstance 方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| if (mbd.getFactoryMethodName() != null) { return instantiateUsingFactoryMethod(beanName, mbd, args); }
boolean resolved = false;
boolean autowireNecessary = false; if (args == null) { synchronized (mbd.constructorArgumentLock) { if (mbd.resolvedConstructorOrFactoryMethod != null) { resolved = true; autowireNecessary = mbd.constructorArgumentsResolved; } } }
if (resolved) { if (autowireNecessary) { return autowireConstructor(beanName, mbd, null, null); } else { return instantiateBean(beanName, mbd); } }
Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName); if (ctors != null || mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR || mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) { return autowireConstructor(beanName, mbd, ctors, args); }
ctors = mbd.getPreferredConstructors(); if (ctors != null) { return autowireConstructor(beanName, mbd, ctors, null); }
return instantiateBean(beanName, mbd);
|
七、finishRefresh
最后一步