简介:从底层方向详解了Spring中的核心内容IoC容器的原理
Spirng的IOC是Spring的核心之一,容器的创建和刷新主要的流程如下:
创建和刷新的过程集中在AbstractApplicationContext
类的refresh
方法中,该方法定义如下
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");
// 进行容器刷新前的欲处理工作
prepareRefresh();
// 通过GenericApplicationContext类的构造方法创建容器,并且进行一些必要的设置工作
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// 对创建的容器进行一些主要组件的注册
prepareBeanFactory(beanFactory);
try {
// 容器的前置处理器 空方法 主要用于子类实现之后注册一些必备的组件
postProcessBeanFactory(beanFactory);
StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");
// 对容器中的bean进行循环执行bean的前置处理器方法 (按照优先级)
invokeBeanFactoryPostProcessors(beanFactory);
// 容器的后置处理器
registerBeanPostProcessors(beanFactory);
beanPostProcess.end();
// 初始化MessageSource工作包括:国际化,消息绑定,消息解析等。
initMessageSource();
// 初始化事件派发器
initApplicationEventMulticaster();
// 主要初始化特定上下文子类中的其他特殊 bean。
onRefresh();
// 将所有的监听器注册到容器中
registerListeners();
// 实例化所有的单实例bean
finishBeanFactoryInitialization(beanFactory);
// 最后一步:完成容器的刷新工作 发布相应的事件。
finishRefresh();
}
catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
// 如果抛出异常,则销毁已经创建的单例以节约资源。
destroyBeans();
// 重写设置激活状态
cancelRefresh(ex);
// 将异常抛出,传播给调用者。
throw ex;
}
finally {
// 重置 Spring 核心缓存
// might not ever need metadata for singleton beans anymore...
resetCommonCaches();
contextRefresh.end();
}
}
}
prepareRefresh
方法这个方法是进行容器刷新前的预处理工作
的,该方法的定义如下
protected void prepareRefresh() {
this.startupDate = System.currentTimeMillis(); //记录启动时间
this.closed.set(false); //是否关闭
this.active.set(true); //是否激活
if (this.logger.isDebugEnabled()) {
if (this.logger.isTraceEnabled()) {
this.logger.trace("Refreshing " + this);
} else {
this.logger.debug("Refreshing " + this.getDisplayName());
}
}
this.initPropertySources(); //设置属性源
this.getEnvironment().validateRequiredProperties(); //对设置的属性源进行校验
if (this.earlyApplicationListeners == null) {
this.earlyApplicationListeners = new LinkedHashSet(this.applicationListeners);
} else {
this.applicationListeners.clear();
this.applicationListeners.addAll(this.earlyApplicationListeners);
}
this.earlyApplicationEvents = new LinkedHashSet();
}
initPropertySources
方法,通过Debug我们发现,这个方法是一个空方法 ,这个方法在此处什么也不做,但是我们可以继承这个类来自定义个性化的属性配置this.getEnvironment().validateRequiredProperties()
这个方法主要对上一步的属性配置进行校验。earlyApplicationListeners
进行非null校验通过obtainFreshBeanFactory
方法获取了BeanFactory
该方法的定义如下
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
refreshBeanFactory(); //容器创建
return getBeanFactory();//返回刷新后的容器
}
方法首先调用refreshBeanFactory();
进行容器的刷新工作,通过Debug,我们发现流程进入了GenericApplicationContext
类中,我们发现此类的构造方法创建了一个BeanFactory
,
public GenericApplicationContext() {
this.beanFactory = new DefaultListableBeanFactory();
}
我们回到refreshBeanFactory
方法,此方法中,设置了Serialization
的id,然后将创建的BeanFactory
返回给调用者,
protected final void refreshBeanFactory() throws IllegalStateException {
if (!this.refreshed.compareAndSet(false, true)) {
throw new IllegalStateException(
"GenericApplicationContext does not support multiple refresh attempts: just call 'refresh' once");
}
this.beanFactory.setSerializationId(getId()); //设置serializationId
}
也就是之前的AbstractApplicationContext
类中的refresh
方法的第五行,流程回来。
对BeanFacotory
进行预处理的工作,对其进行一些设置。
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
// Tell the internal bean factory to use the context's class loader etc.
beanFactory.setBeanClassLoader(getClassLoader()); //设置类加载器
if (!shouldIgnoreSpel) {
beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
}
beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment())); //设置支持的表达式解析器
// 设置忽略的自动装配接口
beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);
beanFactory.ignoreDependencyInterface(ApplicationStartupAware.class);
//注册可以解析的自动装配 包括BeanFactory,ResourceLoader,ResourceLoader,ApplicationContext等。
beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
beanFactory.registerResolvableDependency(ResourceLoader.class, this);
beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
beanFactory.registerResolvableDependency(ApplicationContext.class, this);
// 添加后置处理器
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));
// Detect a LoadTimeWeaver and prepare for weaving, if found.
if (!NativeDetector.inNativeImage() && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
// Set a temporary ClassLoader for type matching.
beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
}
// 注册环境变量
if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {
beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
}
if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
}
if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {
beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
}
if (!beanFactory.containsLocalBean(APPLICATION_STARTUP_BEAN_NAME)) {
beanFactory.registerSingleton(APPLICATION_STARTUP_BEAN_NAME, getApplicationStartup());
}
}
refresh
的16行 postProcessBeanFactory(beanFactory); 方法,此方法是一个空方法 主要用于子类实现之后对容器进行前置工作refresh
方法的18行,在Bean Factory标准初始流程之后执行refresh
20行,invokeBeanFactoryPostProcessors方法被执行 该方法定义如下protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());
if (!NativeDetector.inNativeImage() && beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
}
}
第一行调用invokeBeanFactoryPostProcessors
方法,此方法会实例化并调用所有已注册的 BeanFactoryPostProcessor bean,如果给出则遵守显式顺序。 必须在单例实例化之前调用。
该方法的定义如下
```java
public static void invokeBeanFactoryPostProcessors(
ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {
Set<String> processedBeans = new HashSet<>();
//判断beanfactory的类型 如果是BeanDefinitionRegistry则对其进行遍历并执行bean的前置处理器
if (beanFactory instanceof BeanDefinitionRegistry) {
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();
// 循环将传入的beanFacotory中bean的前置处理器添加到list集合中
for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
BeanDefinitionRegistryPostProcessor registryProcessor =
(BeanDefinitionRegistryPostProcessor) postProcessor;
registryProcessor.postProcessBeanDefinitionRegistry(registry);
registryProcessors.add(registryProcessor);
}
else {
regularPostProcessors.add(postProcessor);
}
List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();
// 通过优先级进行排序
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
// 首先判断bean是否实现了PriorityOrdered接口
if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);
}
}
sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);
//执行bean的前置处理器
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry, beanFactory.getApplicationStartup());
currentRegistryProcessors.clear();
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
// 其次判断是否实现了Ordered接口
if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);
}
}
sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);
//执行bean的前置处理器
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry, beanFactory.getApplicationStartup());
currentRegistryProcessors.clear();
boolean reiterate = true;
while (reiterate) {
reiterate = false;
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
if (!processedBeans.contains(ppName)) {
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);
reiterate = true;
}
}
sortPostProcessors(currentRegistryProcessors, beanFactory)
registryProcessors.addAll(currentRegistryProcessors);
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry, beanFactory.getApplicationStartup());
currentRegistryProcessors.clear();
}
//执行没有实现任何优先级的bean的前置处理器;
invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
}
else {
invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
}
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);
//判断beanfactory的类型 如果是PriorityOrdered则对其进行遍历并执行bean的前置处理器
List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
List<String> orderedPostProcessorNames = new ArrayList<>();
List<String> nonOrderedPostProcessorNames = new ArrayList<>();
for (String ppName : postProcessorNames) {
if (processedBeans.contains(ppName)) {
}
else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
}
else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
orderedPostProcessorNames.add(ppName);
}
else {
nonOrderedPostProcessorNames.add(ppName);
}
}
// 首先执行priorityOrderedPostProcessors接口的bean的前置处理器
sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);
//判断beanfactory的类型 如果是BeanFactoryPostProcessor则对其进行遍历并执行bean的前置处理器
List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
for (String postProcessorName : orderedPostProcessorNames) {
orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
}
sortPostProcessors(orderedPostProcessors, beanFactory);
invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);
List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());
for (String postProcessorName : nonOrderedPostProcessorNames) {
nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
}
invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);
//清除缓存的合并 bean 定义,因为后处理器可能已经修改了原始元数据,例如替换值中的占位符..
beanFactory.clearMetadataCache();
}
```
refresh
方法的23行。registerBeanPostProcessors(beanFactory);
对容器中的bean进行后置处理 方法的定义如下
protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);
}
继续进入registerBeanPostProcessors
方法
```java
public static void registerBeanPostProcessors(
ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {
//获取容器中所有的postProcessorNames
String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);
int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
// 加入自己的后置处理器
beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));
//分离实现 PriorityOrdered 的 BeanPostProcessor
List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
List<BeanPostProcessor> internalPostProcessors = new ArrayList<>();
List<String> orderedPostProcessorNames = new ArrayList<>();
List<String> nonOrderedPostProcessorNames = new ArrayList<>();
// 循环所有的后置处理器 通过MergedBeanDefinitionPostProcessor接口和Ordered接口进行优先级排序
for (String ppName : postProcessorNames) {
if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
priorityOrderedPostProcessors.add(pp);
if (pp instanceof MergedBeanDefinitionPostProcessor) {
internalPostProcessors.add(pp);
}
}
else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
orderedPostProcessorNames.add(ppName);
}
else {
nonOrderedPostProcessorNames.add(ppName);
}
}
// 首先,注册实现 PriorityOrdered 的 BeanPostProcessors。
sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);
List<BeanPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
for (String ppName : orderedPostProcessorNames) {
BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
orderedPostProcessors.add(pp);
if (pp instanceof MergedBeanDefinitionPostProcessor) {
internalPostProcessors.add(pp);
}
}
//接下来,注册实现 Ordered 的 BeanPostProcessors。
sortPostProcessors(orderedPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, orderedPostProcessors);
// 现在,注册所有常规 BeanPostProcessor。
List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());
for (String ppName : nonOrderedPostProcessorNames) {
BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
nonOrderedPostProcessors.add(pp);
if (pp instanceof MergedBeanDefinitionPostProcessor) {
internalPostProcessors.add(pp);
}
}
registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);
// 最后,重新注册所有内部 BeanPostProcessor。
sortPostProcessors(internalPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, internalPostProcessors);
// 将用于检测内部 bean 的后处理器重新注册为 ApplicationListeners,
// 将其移动到处理器链的末端(用于获取代理等)。
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
}
8. ### 来到`refresh`方法的27行。 initMessageSource()
初始化MessageSource工作包括:国际化,消息绑定,消息解析等。
定义如下
```java
protected void initMessageSource() {
//获取容器
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
//判断容器中是否有id为MESSAGE_SOURCE_BEAN_NAME的组件
if (beanFactory.containsLocalBean(MESSAGE_SOURCE_BEAN_NAME)) {
//如果有 赋值给自己的属性
this.messageSource = beanFactory.getBean(MESSAGE_SOURCE_BEAN_NAME, MessageSource.class);
// 使 MessageSource 知道父 MessageSource。
if (this.parent != null && this.messageSource instanceof HierarchicalMessageSource) {
HierarchicalMessageSource hms = (HierarchicalMessageSource) this.messageSource;
if (hms.getParentMessageSource() == null) {
// 如果容器中没有id为MESSAGE_SOURCE_BEAN_NAME的组件 则创建一个默认的
hms.setParentMessageSource(getInternalParentMessageSource());
}
}
//用于debug环境下打印提示信息
if (logger.isTraceEnabled()) {
logger.trace("Using MessageSource [" + this.messageSource + "]");
}
}
else {
//给容器中注册空的 MessageSource 使得容器可以接受 getMessage 的调用
DelegatingMessageSource dms = new DelegatingMessageSource();
dms.setParentMessageSource(getInternalParentMessageSource());
this.messageSource = dms;
beanFactory.registerSingleton(MESSAGE_SOURCE_BEAN_NAME, this.messageSource);
if (logger.isTraceEnabled()) {
logger.trace("No '" + MESSAGE_SOURCE_BEAN_NAME + "' bean, using [" + this.messageSource + "]");
}
}
}
refresh
方法的30行 initApplicationEventMulticaster();protected void initApplicationEventMulticaster() {
//获取容器
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
//如果容器中存在APPLICATION_EVENT_MULTICASTER_BEAN_NAME
if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {
//将自己的组件设置为容器中的组件
this.applicationEventMulticaster =
beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
if (logger.isTraceEnabled()) {
logger.trace("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]");
}
}
else {
//如果上一步灭有配置 则创建一个默认的
this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
// 并且注册到容器中
beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);
if (logger.isTraceEnabled()) {
logger.trace("No '" + APPLICATION_EVENT_MULTICASTER_BEAN_NAME + "' bean, using " +
"[" + this.applicationEventMulticaster.getClass().getSimpleName() + "]");
}
}
}
refresh
方法的32行 onRefresh(); 主要初始化特定上下文子类中的其他特殊 bean。
protected void onRefresh() throws BeansException {
// For subclasses: do nothing by default.
}
空实现,留给子类重写,然后在容器刷新的时候,自定义逻辑。
refresh
方法的36行 registerListeners();将所有的监听器注册到容器中
protected void registerListeners() {
// 拿到所有的监听器
for (ApplicationListener<?> listener : getApplicationListeners()) {
getApplicationEventMulticaster().addApplicationListener(listener);
}
// 将每个监听器添加到事件派发器中
String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
for (String listenerBeanName : listenerBeanNames) {
getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
}
// 派发之前步骤产生的事件
Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;
this.earlyApplicationEvents = null;
if (!CollectionUtils.isEmpty(earlyEventsToProcess)) {
for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
getApplicationEventMulticaster().multicastEvent(earlyEvent);
}
}
}
refresh
方法的39行 finishBeanFactoryInitialization实例化所有的单实例bean
protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
//转换
if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) &&
beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) {
beanFactory.setConversionService(
beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class));
}
if (!beanFactory.hasEmbeddedValueResolver()) {
beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal));
}
//尽早初始化 LoadTimeWeaverAware bean 以允许尽早注册它们的转换器。
String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
for (String weaverAwareName : weaverAwareNames) {
getBean(weaverAwareName);
}
// 停止使用临时 ClassLoader 进行类型匹配
beanFactory.setTempClassLoader(null);
//允许缓存所有 bean 定义元数据 因为是单实例模式
beanFactory.freezeConfiguration();
// 初始化剩下的单实例bean
beanFactory.preInstantiateSingletons();
}
在此方法中,所有的代码都是为了最后一步初始化单实例bean,26行中调用了容器的preInstantiateSingletons
方法 定义如下
public void preInstantiateSingletons() throws BeansException {
if (this.logger.isTraceEnabled()) {
this.logger.trace("Pre-instantiating singletons in " + this);
}
// 首先获取所有的bean
List<String> beanNames = new ArrayList(this.beanDefinitionNames);
Iterator var2 = beanNames.iterator();
// 遍历所有的bean 依次实例化
while(true) {
String beanName;
Object bean;
do {
while(true) {
RootBeanDefinition bd;
do {
do {
do {
if (!var2.hasNext()) {
var2 = beanNames.iterator();
while(var2.hasNext()) {
beanName = (String)var2.next();
Object singletonInstance = this.getSingleton(beanName);
//如果bean是单实例的
if (singletonInstance instanceof SmartInitializingSingleton) {
StartupStep smartInitialize = this.getApplicationStartup().start("spring.beans.smart-initialize").tag("beanName", beanName);
SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton)singletonInstance;
if (System.getSecurityManager() != null) {
AccessController.doPrivileged(() -> {
smartSingleton.afterSingletonsInstantiated();
return null;
}, this.getAccessControlContext());
} else {
smartSingleton.afterSingletonsInstantiated();
}
smartInitialize.end();
}
}
return;
}
beanName = (String)var2.next();
bd = this.getMergedLocalBeanDefinition(beanName);
} while(bd.isAbstract());
} while(!bd.isSingleton());
} while(bd.isLazyInit());
if (this.isFactoryBean(beanName)) {
bean = this.getBean("&" + beanName);
break;
}
this.getBean(beanName);
}
} while(!(bean instanceof FactoryBean));
FactoryBean<?> factory = (FactoryBean)bean;
boolean isEagerInit;
if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
SmartFactoryBean var10000 = (SmartFactoryBean)factory;
((SmartFactoryBean)factory).getClass();
isEagerInit = (Boolean)AccessController.doPrivileged(var10000::isEagerInit, this.getAccessControlContext());
} else {
//判断是否是SmartFactoryBean接口的bean
isEagerInit = factory instanceof SmartFactoryBean && ((SmartFactoryBean)factory).isEagerInit();
}
// 如果是 则调用ban自己重写的getObject方法创建组件
if (isEagerInit) {
this.getBean(beanName);
}
}
}
对所有的Bean进行实例化主要的步骤如下:
SmartFactoryBean
接口,则进行实例化,SmartFactoryBean
,则调用它重写的getObjece()方法,实例化组件。
getBean(name)
方法,73行所示doGetBean(name,null,null,false)
方法
createBean
方法
beanPostProcessor
创建代理对象的机会。doCreateBean(name)
createBeanInstance
方法,此时,Debug进入bean的构造方法,实例出bean对象。populateBean()
applyPropertyValues()
方法 为bean的属性利用反射机制
中获取的set方法进行赋值。是否实现了执行init接口
或者是否在注解中指定了Init方法
,如果有,则回调bean的初始化方法。即自定义的Init方法。doCreateBean(name)
执行完毕,创建bean完成。是否实现了销毁接口
或者是否在注解中指定了销毁方法
,如果有,注册bean的销毁方法(非执行)。doCreateBean(name)
结束,bean对象创建完成。createBean
方法完成,bean对象创建完成。将其添加到单实例bean的列表中。继续循环迭代,直到数组beanNames
中的所有bean创建完毕。
refresh
方法的39行 finishRefresh();最后一步:完成容器的初始化工作,发布相应的事件。
```java
protected void finishRefresh() {
// 清除上下文级资源缓存
clearResourceCaches();
//初始化和生命周期有关的后置处理器。
initLifecycleProcessor();
getLifecycleProcessor().onRefresh();
//发布最终事件。
publishEvent(new ContextRefreshedEvent(this));
if (!NativeDetector.inNativeImage()) {
LiveBeansView.registerApplicationContext(this);
}
}
```
AutowiredAnnotationBeanPostProcessor
Bean用来处理自动注入的功能。AnnotationAwareAspectJAutoProxyCreator
Bean用来AOP切面功能。ApplicationListener
: 事件监听ApplicationEventMuiticaster
: 事件派发