org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBean
1.实例化bean:
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#createBeanInstance
-》org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#autowireConstructor
-》org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#instantiateBean
2.填充属性:
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#populateBean
3.初始化bean:
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#initializeBean(java.lang.String, java.lang.Object, org.springframework.beans.factory.support.RootBeanDefinition)
3.1调用实现了Aware接口的方法:
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#invokeAwareMethods
3.1.1如果Bean实现了BeanNameAware接口的话,将调用Bean的setBeanName(String name)方法,在创建此bean的bean工厂中设置bean的名称
org.springframework.beans.factory.BeanNameAware#setBeanName
3.1.2如果Bean实现了BeanClassLoaderAware接口的话,将调用Bean的setBeanClassLoader(ClassLoader classLoader)方法,用于将类加载器提供给bean实例的回调
org.springframework.beans.factory.BeanClassLoaderAware#setBeanClassLoader
3.1.3如果Bean实现了BeanFactoryAware接口的话,将调用Bean的setBeanFactory(BeanFactory beanFactory)方法,将当前的beanFactory提供给bean实例回调
org.springframework.beans.factory.BeanFactoryAware#setBeanFactory
3.1.4如果Bean实现了ApplicationContextAware接口的话,将调用Bean的setApplicationContext(ApplicationContext applicationContext)方法,将bean所在应用上下文引用传入进来
org.springframework.context.ApplicationContextAware#setApplicationContext
3.2调用所有BeanPostProcesser的postProcessBeforeInitialization(Object bean, String beanName)方法
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#applyBeanPostProcessorsBeforeInitialization
-》org.springframework.beans.factory.config.BeanPostProcessor#postProcessBeforeInitialization
3.3调用初始化方法
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#invokeInitMethods
3.3.1如果Bean实现了InitializingBean接口,将调用Bean的afterPropertiesSet()方法
3.3.2如果Bean使用init-method声明了初始化方法,则调用init-method声明的方法
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#invokeCustomInitMethod
3.4调用所有BeanPostProcesser的postProcessAfterInitialization(Object bean, String beanName)方法
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#applyBeanPostProcessorsAfterInitialization
-》org.springframework.beans.factory.config.BeanPostProcessor#postProcessAfterInitialization
到此bean实例化完成,即bean可以被使用。他们将一直驻留在应用上下文中,直到应用上下文被销毁。
4.如果bean实现了DisposableBean接口,Spring将调用它的destory()接口方法,同样,如果bean使用了destory-method 声明销毁方法,该方法也会被调用
将给定的bean添加到该工厂中的DisposableBean列表org.springframework.beans.factory.support.DefaultSingletonBeanRegistry#disposableBeans中,注册其DisposableBean接口,或者在工厂关闭时调用给定的销毁方法(如果适用)
org.springframework.beans.factory.support.AbstractBeanFactory#registerDisposableBeanIfNecessary
-》org.springframework.beans.factory.support.DefaultSingletonBeanRegistry#registerDisposableBean
-》org.springframework.beans.factory.config.Scope#registerDestructionCallback
结束