spring源码-----主流程

spring源码是一个很有意思的东西,没事可以去读一下,看一下

从最简单的spring源码开始解读,带web.xml配置的那种,在web.xml中有两个很重要的配置,

第一个,spring的listener,第二个就是springmvc的最重要的容器了。

<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
<servlet>
		<servlet-name>spring</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

spring容器的启动,就在ContextLoaderListener,源码分析可以从这里跟进去,找到里面的initWebApplicationContext方法,spring容器初始化就在这里。initWebApplicationContext在父类ContextLoader里,朝下继续,configureAndRefreshWebApplicationContext方法里refresh方法,就到了

AbstractApplicationContext

这就是spring最重要的类了,容器初始化的核心流程


	/*
	 *	该方法是spring容器初始化的核心方法。是spring容器初始化的核心流程,是一个典型的父类模板设计模式的运用
	 *		根据不同的上下文对象,会掉到不同的上下文对象子类方法中
	 *
	 *	核心上下文子类有:
	 *	ClassPathXmlApplicationContext  
	 *	FileSystemXmlApplicationContext
	 *	AnnotationConfigApplicationContext
	 *	EmbeddedWebApplicationContext(springboot)
	 *
	 * */
	@Override
	public void refresh() throws BeansException, IllegalStateException {
		synchronized (this.startupShutdownMonitor) {//加锁,保证容器创建过程中不会被销毁
			//为容器初始化做准备,记录时间,监听之类的
			// Prepare this context for refreshing.
			prepareRefresh();

			/*		

			  1、创建BeanFactory对象
			* 2、xml解析
			* 	传统标签解析:bean、import等
			* 	自定义标签解析 
			* 	自定义标签解析流程:
			* 		a、根据当前解析标签的头信息找到对应的namespaceUri
			* 		b、加载spring所以jar中的spring.handlers文件。并建立映射关系
			* 		c、根据namespaceUri从映射关系中找到对应的实现了NamespaceHandler接口的类
			* 		d、调用类的init方法,init方法是注册了各种自定义标签的解析类
			* 		e、根据namespaceUri找到对应的解析类,然后调用paser方法完成标签解析
			*
			* 3、把解析出来的xml标签封装成BeanDefinition对象
			* */
			// Tell the subclass to refresh the internal bean factory.
			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

			// Prepare the bean factory for use in this context.
			prepareBeanFactory(beanFactory);

			try {
				// Allows post-processing of the bean factory in context subclasses.
				postProcessBeanFactory(beanFactory);

				// Invoke factory processors registered as beans in the context.
				invokeBeanFactoryPostProcessors(beanFactory);

				// Register bean processors that intercept bean creation.
				registerBeanPostProcessors(beanFactory);

				// Initialize message source for this context.
				initMessageSource();

				// Initialize event multicaster for this context.
				initApplicationEventMulticaster();

				// Initialize other special beans in specific context subclasses.
				onRefresh();

				// Check for listener beans and register them.
				registerListeners();

				// Instantiate all remaining (non-lazy-init) singletons.
                //这里是准备完成后正式初始化初始化bean
				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.
				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、付费专栏及课程。

余额充值