刷新前的预处理:检验属性的合法等,保存容器中的一些早期的事件(new LinkedHashSet())
获取BeanFactory:this.beanFactory = new DefaultListableBeanFactory()
BeanFactory进行一些设置:设置BeanFactory的类加载器、支持表达式解析器
BeanFactory准备工作完成后进行的后置处理工作
执行BeanFactoryPostProcessor的方法
注册BeanPostProcessor(Bean的后置处理器)【 intercept bean creation】
不同接口类型的BeanPostProcessor;在Bean创建前后的执行时机是不一样的
BeanPostProcessor、
DestructionAwareBeanPostProcessor、
InstantiationAwareBeanPostProcessor、
SmartInstantiationAwareBeanPostProcessor、
MergedBeanDefinitionPostProcessor【internalPostProcessors】、
初始化MessageSource组件(做国际化功能;消息绑定,消息解析)
初始化事件派发器
子类重写这个方法,在容器刷新的时候可以自定义逻辑
给容器中将所有项目里面的ApplicationListener注册进来;
1、从容器中拿到所有的ApplicationListener
2、将每个监听器添加到事件派发器中;
getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
3、派发之前步骤产生的事件;
初始化所有剩下的单实例bean
完成BeanFactory的初始化创建工作;IOC容器就创建完成
从调用堆栈我们来到finishBeanFactoryInitialization(beanFactory),它的关键逻辑
RootBeanDefinition mbd = this.getMergedLocalBeanDefinition(beanName);
this.registerDependentBean(dep, beanName);
//上面这个方法registerDependentBean的具体实现
public void registerDependentBean(String beanName, String dependentBeanName) {
String canonicalName = this.canonicalName(beanName);
Set dependenciesForBean;
synchronized(this.dependentBeanMap) {
dependenciesForBean = (Set)this.dependentBeanMap.computeIfAbsent(canonicalName, (k) -> {
return new LinkedHashSet(8);
});
if (!dependenciesForBean.add(dependentBeanName)) {
return;
}
}
synchronized(this.dependenciesForBeanMap) {
dependenciesForBean = (Set)this.dependenciesForBeanMap.computeIfAbsent(dependentBeanName, (k) -> {
return new LinkedHashSet(8);
});
dependenciesForBean.add(canonicalName);
}
}
//让BeanPostProcessor先拦截返回代理对象;
beanInstance = this.resolveBeforeInstantiation(beanName, mbdToUse);
//创建Bean实例对象;
if (instanceWrapper == null) {
instanceWrapper = this.createBeanInstance(beanName, mbd, args);
}
继续createBeanInstance 方式继续执行:利用工厂方法或者对象的构造器创建出Bean实例
继续更调用栈走到下图的断点调用,这个时候:SpringCloudClientBeanPostProcessor就在这里被实例的,同时参数SoulSpringCloudConfig和Environment传递进来,
this.populateBean(beanName, mbd, instanceWrapper);
Object wrappedBean = bean;
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = this.applyBeanPostProcessorsBeforeInitialization(bean, beanName);
}
try {
this.invokeInitMethods(beanName, wrappedBean, mbd);
} catch (Throwable var6) {
throw new BeanCreationException(mbd != null ? mbd.getResourceDescription() : null, beanName, "Invocation of init method failed", var6);
}
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = this.applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}
进到applyBeanPostProcessorsAfterInitialization方法
public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName) throws BeansException {
Object result = existingBean;
Object current;
// 拿到所有ioc容器的BeanPostProcessor,遍历当前对象比配是否是BeanPostProcessor的实现类,如果是执行:processor.postProcessAfterInitialization(result, beanName)
for(Iterator var4 = this.getBeanPostProcessors().iterator(); var4.hasNext(); result = current) {
BeanPostProcessor processor = (BeanPostProcessor)var4.next();
current = processor.postProcessAfterInitialization(result, beanName);
if (current == null) {
return result;
}
}
return result;
}
this.registerDisposableBeanIfNecessary(beanName, bean, mbd);
this.addSingleton(beanName, singletonObject);
singletonObjects对象是:MapsingletonObjects = new ConcurrentHashMap(256),即ioc容器就是这些Map
protected void addSingleton(String beanName, Object singletonObject) {
synchronized(this.singletonObjects) {
this.singletonObjects.put(beanName, singletonObject);
this.singletonFactories.remove(beanName);
this.earlySingletonObjects.remove(beanName);
this.registeredSingletons.add(beanName);
}
}