Spring5 源码阅读笔记(1)refresh() 刷新

入口

private ApplicationContext applicationContext 
= new ClassPathXmlApplicationContext("spring.xml");

有了 applicationContet 之后,就可进行 getBean(beanName) 操作。

与 ClassPathXmlApplicationContext 平行的还有:
FileSystemXmlApplicationContext(也是基于配置文件,参数是绝对路径)
AnnotationConfigApplicationContext(基于注解,参数是包名)
EmbeddedWebApplicationContext(SpringBoot用到的)

跟 ClassPathXmlApplicationContext:
在这里插入图片描述
跟 this:
在这里插入图片描述
跟 refresh():
这个方法在父类 AbstractApplicationContext 中

@Override
public void refresh() throws BeansException, IllegalStateException {
	synchronized (this.startupShutdownMonitor) {
	
		//为容器初始化做准备,重要程度:0
		prepareRefresh();

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

		/*
		* 给beanFactory设置一些属性值,重要程度:0
		* */
		prepareBeanFactory(beanFactory);

		try {
			
			// Allows post-processing of the bean factory in context subclasses.
			postProcessBeanFactory(beanFactory);
			
			//对实现了 BeanFactoryPostProcessor 接口的类转变而来的 BeanDefinition
			//进行实例化和调用。为了增修 BeanDefinition。
			invokeBeanFactoryPostProcessors(beanFactory);

			
			//对实现了 BeanFactoryProcessor 接口的类转变而来的 BeanDefinition
			//进行实例化和注册。为了在后面某些 bean 实例化过程中提供操作。
			registerBeanPostProcessors(beanFactory);

			/*
			* 国际化,重要程度:2
			* */
			// Initialize message source for this context.
			initMessageSource();

			//初始化事件管理类
			// Initialize event multicaster for this context.
			initApplicationEventMulticaster();

			//这个方法着重理解模板设计模式,因为在springboot中,这个方法是用来做内嵌tomcat启动的
			// Initialize other special beans in specific context subclasses.
			onRefresh();

			/*
			* 往事件管理类中注册事件类
			* */
			// Check for listener beans and register them.
			registerListeners();


			/*
			* 这个方法是spring中最重要的方法,没有之一
			* 所以这个方法一定要理解要具体看
			* 1、bean实例化过程
			* 2、ioc
			* 3、注解支持
			* 4、BeanPostProcessor的执行
			* 5、Aop的入口
			*
			* */
			// 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.
			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();
		}
	}
}

方法详解链接:
obtainFreshBeanFactory()
invokeBeanFactoryPostProcessors(beanFactory)
registerBeanPostProcessors(beanFactory)

发布了130 篇原创文章 · 获赞 233 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_44367006/article/details/104344734