Spring启动—Bean的初始化顺序

通过这段代码作为分析的入口。以下分析都基于该示例,完整代码见:https://github.com/abelzha/spring-framework

public class InitMain {
	public static void main(String[] args) {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfigInit.class);
		System.out.println(Arrays.asList(context.getBeanFactory().getBeanDefinitionNames()).toString().replaceAll(",", "\n"));

		Person person = context.getBean(Person.class);
		System.out.println(person.getName());
		context.close();
	}
}

1. IOC容器启动—Bean的初始化顺序

在Spring启动的时候会先创建容器、进行Bean的初始化。

1、根据配置类,创建容器,完成添加内置类的beanDefinitionMap

1、创建容器(AnnotationConfigApplicationContext)
容器的中的bean工厂:DefaultListableBeanFactory

2、初始配置信息设置,填充各种map、set.如下图所示
Spring启动—Bean的初始化顺序_第1张图片
现在我们只关注一个map,它就是beanDefinitionMap。
将内置类的beanDefinition放入beanDefinitionMap,以下是该示例的默认添加的5个内置类

1	name:org.springframework.context.annotation.internalConfigurationAnnotationProcessor 
	class[org.springframework.context.annotation.ConfigurationClassPostProcessor]

2	name:org.springframework.context.event.internalEventListenerFactory
	class:[org.springframework.context.event.DefaultEventListenerFactory]

3	name:org.springframework.context.event.internalEventListenerProcessor
	class:[org.springframework.context.event.EventListenerMethodProcessor]

4	name:org.springframework.context.annotation.internalAutowiredAnnotationProcessor 
	class:[org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor]

5	name:org.springframework.context.annotation.internalCommonAnnotationProcessor
	class:[org.springframework.context.annotation.CommonAnnotationBeanPostProcessor]

代码说明:

/**
	 * Register all relevant annotation post processors in the given registry.
	 * @param registry the registry to operate on
	 * @param source the configuration source element (already extracted)
	 * that this registration was triggered from. May be {@code null}.
	 * @return a Set of BeanDefinitionHolders, containing all bean definitions
	 * that have actually been registered by this call
	 */
	public static Set<BeanDefinitionHolder> registerAnnotationConfigProcessors(
			BeanDefinitionRegistry registry, @Nullable Object source) {

		DefaultListableBeanFactory beanFactory = unwrapDefaultListableBeanFactory(registry);
		if (beanFactory != null) {
			if (!(beanFactory.getDependencyComparator() instanceof AnnotationAwareOrderComparator)) {
				beanFactory.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
			}
			if (!(beanFactory.getAutowireCandidateResolver() instanceof ContextAnnotationAutowireCandidateResolver)) {
				beanFactory.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
			}
		}

		Set<BeanDefinitionHolder> beanDefs = new LinkedHashSet<>(8);

		if (!registry.containsBeanDefinition(CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME)) {
			RootBeanDefinition def = new RootBeanDefinition(ConfigurationClassPostProcessor.class);
			def.setSource(source);
			beanDefs.add(registerPostProcessor(registry, def, CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME));
		}

		if (!registry.containsBeanDefinition(AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)) {
			RootBeanDefinition def = new RootBeanDefinition(AutowiredAnnotationBeanPostProcessor.class);
			def.setSource(source);
			beanDefs.add(registerPostProcessor(registry, def, AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
		}

		// Check for JSR-250 support, and if present add the CommonAnnotationBeanPostProcessor.
		if (jsr250Present && !registry.containsBeanDefinition(COMMON_ANNOTATION_PROCESSOR_BEAN_NAME)) {
			RootBeanDefinition def = new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class);
			def.setSource(source);
			beanDefs.add(registerPostProcessor(registry, def, COMMON_ANNOTATION_PROCESSOR_BEAN_NAME));
		}

		// Check for JPA support, and if present add the PersistenceAnnotationBeanPostProcessor.
		if (jpaPresent && !registry.containsBeanDefinition(PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME)) {
			RootBeanDefinition def = new RootBeanDefinition();
			try {
				def.setBeanClass(ClassUtils.forName(PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME,
						AnnotationConfigUtils.class.getClassLoader()));
			}
			catch (ClassNotFoundException ex) {
				throw new IllegalStateException(
						"Cannot load optional framework class: " + PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME, ex);
			}
			def.setSource(source);
			beanDefs.add(registerPostProcessor(registry, def, PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME));
		}

		if (!registry.containsBeanDefinition(EVENT_LISTENER_PROCESSOR_BEAN_NAME)) {
			RootBeanDefinition def = new RootBeanDefinition(EventListenerMethodProcessor.class);
			def.setSource(source);
			beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_PROCESSOR_BEAN_NAME));
		}

		if (!registry.containsBeanDefinition(EVENT_LISTENER_FACTORY_BEAN_NAME)) {
			RootBeanDefinition def = new RootBeanDefinition(DefaultEventListenerFactory.class);
			def.setSource(source);
			beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_FACTORY_BEAN_NAME));
		}

		return beanDefs;
	}

2、添加传入的配置类的beanDefinitionMap

将AppConfigInit的beanDefinition放入beanDefinitionMap

至此,Spring启动只完成了beanFactory的实例化,添加内置bean和传入的配置类到beanDefinitionMap中,还没有开始扫描文件。

3、生成内置bean实例和业务bean实例

public void refresh() throws BeansException, IllegalStateException {
		synchronized (this.startupShutdownMonitor) {
			// 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.
				//允许在context子类中对bean工厂进行后处理。空方法
				postProcessBeanFactory(beanFactory);

				// Invoke factory processors registered as beans in the context.
				//调用工厂后置处理器,生成内置处理器的实例,并执行后置处理器的方法,从而通过扫描等获得其他业务bean的beanDefinition,并放入beanDefinitionMap
				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.
				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();
			}
		}
	}

1、调用工厂后置处理器,生成内置处理器的实例,并执行后置处理器的方法,从而通过扫描等获得其他业务bean的beanDefinition,并放入beanDefinitionMap

//调用工厂后置处理器,生成内置处理器的实例,并执行工厂后置处理器的方法,从而通过扫描等获得其他业务bean的beanDefinition,并放入beanDefinitionMap
invokeBeanFactoryPostProcessors(beanFactory);

2、从beanDefinitionNames的map中找出BeanPostProcessor,排序后,添加到bean工厂的beanPostProcessors Map中。
由registerBeanPostProcessors(beanFactory)来初始化BeanPostProcessor以及用户选择开启的功能(例如@EnableAspectJAutoProxy)而产生的BeanPostProcessor。

//注册beanPostProcessors
registerBeanPostProcessors(beanFactory);

1)先获取ioc容器中已经定义的需要创建对象的所有BeanPostProcessor
2)给容器中添加别的BeanPostProcessor(new BeanPostProcessorChecker)
3)优先注册实现了PriorityOrdered接口的BeanPostProcessor
4)再给容器中注册实现了Ordered接口的BeanPostProcessor
5)注册没有实现优先级接口的BeanPostProcessor
注册BeanPostProcessor,实际上就是创建BeanPostProcessor对象,保存在容器beanFactory 。(List beanPostProcessors)
创建BeanPostProcessor对象的过程和创建普通业务bean使用的代码是一样的

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBean

3、实例化所有非懒加载的单实例,包括内置类、传入的配置类、普通业务类

	/**
	 * Central method of this class: creates a bean instance,
	 * populates the bean instance, applies post-processors, etc.
	 * @see #doCreateBean
	 */
	@Override
	protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
			throws BeanCreationException {

		if (logger.isTraceEnabled()) {
			logger.trace("Creating instance of bean '" + beanName + "'");
		}
		RootBeanDefinition mbdToUse = mbd;

		// Make sure bean class is actually resolved at this point, and
		// clone the bean definition in case of a dynamically resolved Class
		// which cannot be stored in the shared merged bean definition.
		Class<?> resolvedClass = resolveBeanClass(mbd, beanName);
		if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {
			mbdToUse = new RootBeanDefinition(mbd);
			mbdToUse.setBeanClass(resolvedClass);
		}

		// Prepare method overrides.
		try {
			mbdToUse.prepareMethodOverrides();
		}
		catch (BeanDefinitionValidationException ex) {
			throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(),
					beanName, "Validation of method overrides failed", ex);
		}

		try {
			// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
			// AOP的动态代理对象在此生成
			Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
			if (bean != null) {
				return bean;
			}
		}
		catch (Throwable ex) {
			throw new BeanCreationException(mbdToUse.getResourceDescription(), beanName,
					"BeanPostProcessor before instantiation of bean failed", ex);
		}

		try {
			// 创建bean实例
			Object beanInstance = doCreateBean(beanName, mbdToUse, args);
			if (logger.isTraceEnabled()) {
				logger.trace("Finished creating instance of bean '" + beanName + "'");
			}
			return beanInstance;
		}
		catch (BeanCreationException | ImplicitlyAppearedSingletonException ex) {
			// A previously detected exception with proper bean creation context already,
			// or illegal singleton state to be communicated up to DefaultSingletonBeanRegistry.
			throw ex;
		}
		catch (Throwable ex) {
			throw new BeanCreationException(
					mbdToUse.getResourceDescription(), beanName, "Unexpected exception during bean creation", ex);
		}
	}

(AOP动态代理对象在此生成,在bean实例对象创建之前)。

// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
Object bean = resolveBeforeInstantiation(beanName, mbdToUse);

创建并注册Bean的过程,这个过程是一个通用的过程。
(org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBean方法的功能)

1、创建bean的实例 	createBeanInstance
2、MergedBeanDefinitionPostProcessor的后置处理器的处理
3、populateBean;给bean的填充各种属性 		   
4、initializebean;初始化Bean; 			
	1invokeAwaremethods();处理bean Aware接口的方法回调 			
	2)applyBeanPostProcessorsBeforeInitialization 			
	3invokeInitMethods();执行自定义的初始化方法 			
	4)applyBeanPostProcessorsAfterInitialization

你可能感兴趣的:(spring,spring,bean初始化)