spring加载流程之refresh()

spring加载流程之refresh

    • AbstractApplicationContext
      • refresh()之prepareRefresh()
      • refresh()之obtainFreshBeanFactory()
      • refresh()之prepareBeanFactory(beanFactory)

缘起: 到这里,上下文构造函数中就剩refresh();方法没有执行了,这是spring重中之重的方法
1.spring加载流程之AnnotatedBeanDefinitionReader
2.spring加载流程之ClassPathBeanDefinitionScanner
3.spring加载流程之构造函数中的register(annotatedClasses)

public AnnotationConfigApplicationContext(Class... annotatedClasses) {
		/**
		 * 这里由于他有父类,所以会先调用父类的构造方法:
		 * 看源码得知初始化了DefaultListableBeanFactory
		 *
		 * 然后才调用自己的构造方法:
		 * 1.创建一个读取注解的Bean定义读取器
		 * 	将bean读取完后,会调用DefaultListableBeanFactory注册这个bean
		 * 2.创建BeanDefinition扫描器
		 *  可以用来扫描包或者类,继而转换为bd
		 */

		this();
		register(annotatedClasses);
		refresh();
	}

AbstractApplicationContext

refresh();方法的执行流程在父类AbstractApplicationContext

public void refresh() throws BeansException, IllegalStateException {
		synchronized (this.startupShutdownMonitor) {
			// Prepare this context for refreshing.

			/**
			 * 准备工作:
			 * 设置启动时间、是否激活标识位
			 * 初始化属性源(property source)配置
			 */
			prepareRefresh();

			// Tell the subclass to refresh the internal bean factory.
			/**
			 * 告诉子类刷新内部bean工厂
			 * 拿到DefaultListableBeanFactory,供后面方法调用
			 */
			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

			// Prepare the bean factory for use in this context.
			/**
			 * 准备bean工厂
			 */
			prepareBeanFactory(beanFactory);

			try {
				// Allows post-processing of the bean factory in context subclasses.
				/**
				 * 这个方法在当前版本没有实现
				 * 可能在spring后面的版本会去扩展
				 */
				postProcessBeanFactory(beanFactory);

				// Invoke factory processors registered as beans in the context.
				/**
				 * 在上下文中调用注册为bean的工厂处理器
				 *
				 * 添加BeanPostProcessor
				 * 如果发现loadTimeWeaver的Bean
				 */
				invokeBeanFactoryPostProcessors(beanFactory);

				// Register bean processors that intercept bean creation.
				/**
				 * 注册BeanPostProcessor
				 * 自定义以及spring内部的
				 */
				registerBeanPostProcessors(beanFactory);

				// Initialize message source for this context.
				/**
				 * 国际化支持,不关心
				 */
				initMessageSource();

				// Initialize event multicaster for this context.
				/**
				 * 初始化事件监听多路广播器
				 */
				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.
				/**
				 * 实例化所有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();
			}
		}
	}

refresh()之prepareRefresh()

/**
 * 准备工作:
 * 设置启动时间、是否激活标识位
 * 初始化属性源(property source)配置
 */
prepareRefresh();
protected void prepareRefresh() {
		this.startupDate = System.currentTimeMillis();
		this.closed.set(false);
		this.active.set(true);

		if (logger.isInfoEnabled()) {
			logger.info("Refreshing " + this);
		}

		// initialize any placeholder property sources in the context environment
		/**
		 * 在上下文环境中初始化任何占位符属性源
		 * 这个方法目前没有子类去实现
		 * 估计spring后期版本有子类去实现
		 */
		initPropertySources();

		// Validate that all properties marked as required are resolvable
		// see ConfigurablePropertyResolver#setRequiredProperties
		getEnvironment().validateRequiredProperties();

		// Allow for the collection of early ApplicationEvents,
		// to be published once the multicaster is available...
		this.earlyApplicationEvents = new LinkedHashSet<>();
	}

refresh()之obtainFreshBeanFactory()

/**
* 告诉子类刷新内部bean工厂
 * 拿到DefaultListableBeanFactory,供后面方法调用
 */
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
		refreshBeanFactory();
		ConfigurableListableBeanFactory beanFactory = getBeanFactory();
		if (logger.isDebugEnabled()) {
			logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);
		}
		return beanFactory;
	}

getBeanFactory();是一个抽象方法(采用了模板模式),子类GenericApplicationContext实现了该方法
在这里插入图片描述
就是获取它自己实例化的beanFactory也就是DefaultListableBeanFactory

public final ConfigurableListableBeanFactory getBeanFactory() {
		return this.beanFactory;
	}

refresh()之prepareBeanFactory(beanFactory)

/**
* 准备bean工厂
 */
prepareBeanFactory(beanFactory);

refresh()方法里面第一个比较重要的方法spring加载流程refresh之prepareBeanFactory(beanFactory)

你可能感兴趣的:(spring源码)