1. 程式人生 > >從原始碼角度看Spring生命週期(官方最全)

從原始碼角度看Spring生命週期(官方最全)

Spring在beanfactory中給出了spring的生命週期的list列表
一、bean初始化前的處理

Bean factory implementations should support the standard bean lifecycle interfaces
as far as possible. The full set of initialization methods and their standard order is:
 * 1. BeanNameAware's {@code setBeanName} 在建立這個bean的工廠中設定bean的名字
 * 2.
BeanClassLoaderAware's {@code setBeanClassLoader} 給bean提供類載入器 * 3. BeanFactoryAware's {@code setBeanFactory}> 給Bean提供beanfactory * 4. ResourceLoaderAware's {@code setResourceLoader} 設定資源解析器支隊applicaitonContext容器有用 * (only applicable when running in an application context) * 5. ApplicationEventPublisherAware's {@code setApplicationEventPublisher} 設定事件釋出 * (only applicable when
running in an application context) * 6. MessageSourceAware's {@code setMessageSource} //設定資源處理 * (only applicable when running in an application context) * 7. ApplicationContextAware's {@code setApplicationContext} //設定上下文處理器 * (only applicable when running in an application context) * 8. ServletContextAware's {@code setServletContext} //設定servlet上下文只對webapplicationcontext起作用
* (only applicable when running in a web application context) * 9. {@code postProcessBeforeInitialization} methods of BeanPostProcessors //設定bean前置處理

二、bean的初始化(繼承initializingbean介面或者自定義init-method方法)

 * 10. InitializingBean's {@code afterPropertiesSet} 
 * 11. a custom init-method definition
 * 12. {@code postProcessAfterInitialization} methods of BeanPostProcessors

三、bean的銷燬(繼承disposableBean介面或者自定義destroy-method方法)

 * On shutdown of a bean factory, the following lifecycle methods apply:
 * 1. DisposableBean's {@code destroy}
 * 2. a custom destroy-method definition