Spring AbstractApplicationContext源码介绍

       Spring的核心思想就是IOC(控制反转)或者说是DI(依赖注入)。是定义一个对象依赖的过程(通俗的说原先由程序员创建对象现在交给Spring进行创建和管理)。对象只和构造参数,工厂方法参数,对象实例属性或工厂返回相关。

       IoC最大的好处就是“解耦”。

AbstractApplicationContext中的refresh方法解析:

//容器创建 容器初始化
	//spring容器中构建过程都是同步的
	//synchronized实现同步  为了保证容器构建的过程中不出现多线程资源冲突问题
	@Override
	public void refresh() throws BeansException, IllegalStateException {
		synchronized (this.startupShutdownMonitor) {
			// Prepare this context for refreshing.
			//上下文的预加载 解析配置文件
			prepareRefresh();

			// Tell the subclass to refresh the internal bean factory.
			//beanFactory的构建
			//beanFactory是applicationcontext的父接口 是spring框架中的顶级容器工厂对象
			//构建beanFactory的功能就是管理bean对象,没有如aop和propertyplaceholder的加载功能等
			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

			// Prepare the bean factory for use in this context.
			//创建beanFactory管理的bean对象
			prepareBeanFactory(beanFactory);

			try {
				// Allows post-processing of the bean factory in context subclasses.
				//加载配置中Beanfactory无法处理的内容;如:propertyplaceholder的加载
				postProcessBeanFactory(beanFactory);

				// Invoke factory processors registered as beans in the context.
				//将上一步加载内容 作为一个容器可以管理的bean对象注册到ApplicationContext中
				//实质上是将postProcessBeanFactory中加载的内容包装成一个容器ApplicationContext可以管理的bean对象
				invokeBeanFactoryPostProcessors(beanFactory);

				// Register bean processors that intercept bean creation.
				//完成上一步的注册操作
				//配置文件中配置的bean对象创建并注册完成
				registerBeanPostProcessors(beanFactory);

				// Initialize message source for this context.
				//初始化国际化消息源
				initMessageSource();

				// Initialize event multicaster for this context.
				//初始化系统的多播事件(注册事件多播监听如ApplicationEvent等 是spring框架观察者的实现模式)
				initApplicationEventMulticaster();

				// Initialize other special beans in specific context subclasses.
				//初始化主题资源 spring框架提供的视图主题信息
				onRefresh();

				// Check for listener beans and register them.
				//创建自定义的监听器并注册
				registerListeners();

				// Instantiate all remaining (non-lazy-init) singletons.
				//初始化配置中所有的lazy-init=false的bean对象且bean对象必须是singleton
				finishBeanFactoryInitialization(beanFactory);

				// Last step: publish corresponding event.
				//最后一步 发布最终事件生命周期监听事件
				//spring容器1定义了生命周期接口,可以实现容器启动调用初始化,容器销毁之前调用回收资源(Iifecycle接口)
				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.
				destroyBeans();

				// Reset 'active' flag.
				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();
			}
		}
	}

菜鸟第一次写博客 有错误请大神纠正

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值