[Source code analysis] DEBUG Spring IOC source code

SpringIOC

声明: 此博客仅记录本人DUBUG源码步骤

Rough diagram:

Insert picture description here

  1. Load the xml file

Insert picture description here


Insert picture description here

setConfigLocations method

Insert picture description here

  1. Call the Refresh method

Insert picture description here

Refresh method

The prepare* methods are all preparations!

@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.
            // 实例化BeanFactory
			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

			// Prepare the bean factory for use in this context.
            // 准备工作,设置或忽略Factory中的一些对箱子
			prepareBeanFactory(beanFactory);

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

				// Invoke factory processors registered as beans in the context.
                // 实例化并执行BeanFactoryPostProcessors
				invokeBeanFactoryPostProcessors(beanFactory);

				// Register bean processors that intercept bean creation.
                // 注册BeanPostProcessors
				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) {
    
    
				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;
			}
		}
	}

prepareRefresh() method

Insert picture description here

ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); // 创建BeanFactory对象

Insert picture description here

refreshBeanFactory method

Insert picture description here

prepareBeanFactory(beanFactory);

The standard context features of the configuration factory, such as the class loader and post processor of the context.

Insert picture description here

postProcessBeanFactory(beanFactory);

Subclass extension

Insert picture description here

invokeBeanFactoryPostProcessors(beanFactory)

Instantiate and execute BeanFactoryPostProcessors!

Insert picture description here

registerBeanPostProcessors(beanFactory)

Register BeanPostProcessor bean

Insert picture description here

initMessageSource();

International processing!

Insert picture description here

initApplicationEventMulticaster()

Initialize a broadcaster!

Insert picture description here

registerListeners()

Register the listener!

Insert picture description here

finishBeanFactoryInitialization(beanFactory);

Instantiate bean

Insert picture description here

Insert picture description here

@Override
	public void preInstantiateSingletons() throws BeansException {
    
    
		if (this.logger.isDebugEnabled()) {
    
    
			this.logger.debug("Pre-instantiating singletons in " + this);
		}

		// Iterate over a copy to allow for init methods which in turn register new bean definitions.
		// While this may not be part of the regular factory bootstrap, it does otherwise work fine.
		List<String> beanNames = new ArrayList<String>(this.beanDefinitionNames);

		// Trigger initialization of all non-lazy singleton beans...
		for (String beanName : beanNames) {
    
    
			RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
			if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
    
    
				if (isFactoryBean(beanName)) {
    
    
					final FactoryBean<?> factory = (FactoryBean<?>) getBean(FACTORY_BEAN_PREFIX + beanName);
					boolean isEagerInit;
					if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
    
    
						isEagerInit = AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
    
    
							@Override
							public Boolean run() {
    
    
								return ((SmartFactoryBean<?>) factory).isEagerInit();
							}
						}, getAccessControlContext());
					}
					else {
    
    
						isEagerInit = (factory instanceof SmartFactoryBean &&
								((SmartFactoryBean<?>) factory).isEagerInit());
					}
					if (isEagerInit) {
    
    
						getBean(beanName);
					}
				}
				else {
    
    
					getBean(beanName);
				}
			}
		}

Insert picture description here

Insert picture description here

In the doGetBean method

Insert picture description here

In createBean method

Insert picture description here

Insert picture description here

In the createBeanInstance method, Bean objects are created through a series of reflection logic

Insert picture description here

Use attribute values ​​to populate Bean instances in a given BeanWrapper

Insert picture description here

Insert picture description here

Insert picture description here

At this point, the Bean is created and instantiated!

finishRefresh()

To complete the refresh of this context, call the onRefresh() method of LifecycleProcessor and publish

Insert picture description here

Attachment: Bean's life cycle

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_42380734/article/details/108059099