One source Spring (positioning, loading, registration)

1. Read the Introduction

Spring Framework is the core of the AOP and the IOC, the IOC can be divided into two main processes, the first main line when the container is to start loading for parsing the configuration file, the second instance of the object is to get the main line from the container when the container initialization. This article detailed analysis of the first main line.

2. Container core interface

2.1 BeanFactory

2.1.1 The main methods and properties

 2.1.2 Role

BeanFactory mainly defines the most basic method of the container, obtaining instance, determination examples of types Scope

2.2 ApplicationContext

2.2.1 

 According to the class diagram, you can know, inherited the ApplicationContext BeanFactory, ResourceLoader, MessageSource, ApplicationEventPublisher, so ApplicationContext not only has the function of the container, as well as support functions of different information sources (international), access to resources, applications and other events.

3. Position the load, the main registration process

 

 4. refresh () method Detailed

Tip: This method represents a major operation when the container starts

@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.
			//子类中启动refreshBeanFactory()的地方,同时也是定位,加载,注册的地方
			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.
				//设置BeanFactory的后置处理
				postProcessBeanFactory(beanFactory);

				// Invoke factory processors registered as beans in the context.
				//调用BeanFactory的后处理器
				invokeBeanFactoryPostProcessors(beanFactory);

				// Register bean processors that intercept bean creation.
				//注册Bean的后处理器,在Bean创建过程中调用
				registerBeanPostProcessors(beanFactory);

				// Initialize message source for this context.
				//对上下文中的消息源进行初始化
				initMessageSource();

				// Initialize event multicaster for this context.
				//初始化上下文中的事件机制
				initApplicationEventMulticaster();

				// Initialize other special beans in specific context subclasses.
				//初始化其它的特殊Bean
				onRefresh();

				// Check for listener beans and register them.
				//检查监听Bean并注册
				registerListeners();

				// Instantiate all remaining (non-lazy-init) singletons.
				//实例化所有的(非non-lazy-init)单实例
				finishBeanFactoryInitialization(beanFactory);

				// Last step: publish corresponding event.
				//发布容器事件,结束Refresh过程
				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();
			}
		}

The simplified process

Guess you like

Origin blog.csdn.net/ly853602/article/details/92417229