欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring ApplicationContext

程序员文章站 2022-05-25 12:37:41
...

ApplicationContext 这个是带有上下文的容器

启动过程

以ClassPathXmlApplicationContext为例

public void refresh() throws BeansException, IllegalStateException {
		synchronized (this.startupShutdownMonitor) {
			StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");

			// Prepare this context for refreshing.
			prepareRefresh();

			// 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);

				StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");
				// Invoke factory processors registered as beans in the context.
				invokeBeanFactoryPostProcessors(beanFactory);

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

				// 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.
				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();
				contextRefresh.end();
			}
		}

1.prepareRefresh

在应用刷新前的一些准备工作

2.准备BeanFactory

生成beanfactory
DefaultListableBeanFactory beanFactory = createBeanFactory();

3.设置Beanfactory

prepareBeanFactory(beanFactory);
比如


		// Configure the bean factory with context callbacks.
		beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
		beanFactory.ignoreDependencyInterface(EnvironmentAware.class);

4.注册BeanFactoryPostProcessor

就是这个接口的实现
BeanFactoryPostProcessor

	postProcessBeanFactory(beanFactory);

	StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");
				// Invoke factory processors registered as beans in the context.
   invokeBeanFactoryPostProcessors(beanFactory);

5.注册BeanPostProcessor


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

6.国际化处理

initMessageSource();

7.事件广播器

初始化事件广播器

initApplicationEventMulticaster();

8.注册listener

就是实现ApplicationListener这个接口的bean
监听事件得到通知

	registerListeners();

9.实现化bean

finishBeanFactoryInitialization

10.结束

finishRefresh()

protected void finishRefresh() {
		// Publish the final event.
		发布这个事件,可以在前面实现监听这个事件
		publishEvent(new ContextRefreshedEvent(this));
	}
相关标签: Spring spring