Spring 源码 之Spring IOC 容器 前戏准备工作

这个方法位于:AbstractApplicationContext#refresh,这个方法中总共有15个方法,Spring源码的精髓就是这15个方法中。

@Override
public void refresh() throws BeansException, IllegalStateException { synchronized (this.startupShutdownMonitor) { // Prepare this context for refreshing.
// 准备工作,加载环境变量等操作
// 1、设置容器启动时间
// 2、设置停止状态为false
// 3、设置活跃状态为true
// 4、获取Environment对象,并设置属性值
// 5、设置监听器和事件的集合,模式为空的集合
prepareRefresh(); // Tell the subclass to refresh the internal bean factory.
// 告诉子类刷新内部 bean 工厂, 获取刷新bean的工厂: DefaultListableBeanFactory
// 并且加载BeanDefinition
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); // Prepare the bean factory for use in this context.
// 准备BeanFactory 设置一些属性
prepareBeanFactory(beanFactory); try { // Allows post-processing of the bean factory in context subclasses.
// 允许子类进行扩展BeanFactoryPostProcessor
postProcessBeanFactory(beanFactory); // Invoke factory processors registered as beans in the context.
// 实例化并执行BeanFactoryPostProcessor
invokeBeanFactoryPostProcessors(beanFactory); // Register bean processors that intercept bean creation.
// 实例化并注册BeanPostProcessor
registerBeanPostProcessors(beanFactory); // Initialize message source for this context.
// 国际化设置
initMessageSource(); // Initialize event multicaster for this context.
// 实例化事件多播器
initApplicationEventMulticaster(); // Initialize other special beans in specific context subclasses.
// 初始化特定上下文子类中的其他特殊bean,web容器
onRefresh(); // Check for listener beans and register them.
// 检查listener bean 并注册它们
// 注册监听器
registerListeners(); // Instantiate all remaining (non-lazy-init) singletons.
// 实例化所有剩余的(非惰性初始化)单例。
finishBeanFactoryInitialization(beanFactory); // Last step: publish corresponding event.
// 发布相应的事件
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.
// 销毁Bean
destroyBeans(); // Reset ‘active’ flag.
// 重置 active 标志
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();
}
}
}

前戏准备 prepareRefresh 方法

Spring的前戏准备大概就是做了以下几件事:

  • 设置容器的启动时间

  • 设置容器的停止状态为false

  • 设置容器的激活状态为true

  • 获取环境信息并验证必要的属性

  • 准备监听器和事件的容器

protected void prepareRefresh() { // Switch to active.
// 设置启动时间 设置标识位
this.startupDate = System.currentTimeMillis(); // 设置容器停止标识为false
this.closed.set(false); // 设置容器激活标识为true
this.active.set(true); // Initialize any placeholder property sources in the context environment.
// 初始化上下文环境中的任何占位符属性源
// 留给子类进行扩展,比如添加必须的属性值验证
initPropertySources(); // Validate that all properties marked as required are resolvable:
// see ConfigurablePropertyResolver#setRequiredProperties
// 获取环境对象,并验证需要的属性
getEnvironment().validateRequiredProperties(); // Store pre-refresh ApplicationListeners…
// 准备应用监听器和实践的容器初始化
if (this.earlyApplicationListeners == null) { this.earlyApplicationListeners = new LinkedHashSet<>(this.applicationListeners);
} else { // Reset local application listeners to pre-refresh state.
// 如果不为空,那么就清空掉,并设置新的早期的监听器进去
this.applicationListeners.clear(); this.applicationListeners.addAll(this.earlyApplicationListeners);
} // Allow for the collection of early ApplicationEvents,
// to be published once the multicaster is available…
this.earlyApplicationEvents = new LinkedHashSet<>();
}

这里有个问题就是他的环境信息是何时设置进去的呢?

实际上是在容器启动时调用了父类构造函数时设置进去的, Environment 他是一个接口,他有个重要的实现类叫 StandardEnvironment ,在Spring启动的时候就会使用这个类进行环境信息的加载,最终他会调用到 System#getProperties 和 System#getenv 方法,然后将加载到属性放在Map中进行保存。

大概的流程如下:

image.png

标记的类就是 Environment 环境信息的加载过程调用的类,最终会调用到 System#getProperties 和 System#getenv 方法,然后完成环境信息的加载,主要加载的信息就是系统的环境变量,比如在 Windows 中配置的环境变量或者启动类中使用 -D 参数配置的启动参数都会进行加载到 StandardEnvironment 这个类中,类似于使用- Dxxx.name=123 这种参数会加载到 systemProperties 中,配置的 windows 环境变量会加载 systemEnvironment 中。

这个就是 Spring IOC 创建的第一个方法的前戏准备工作,接下来解读默认的 BeanFactory 实现类 DefaultListableBeanFactory 的创建过程。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值