Spring Bean的生命周期是一个老生常谈的问题,笔者之前因为面试也看过不少相关的文章。发现每个文章写得都不太一样,有的多有的少。最近在重翻Spring源码的时候,发现`org.springframework.beans.factory.BeanFactory`中对整个过程在注释中写得非常详细,遂整理下来,方便后续查看。
show me the code
代码语言:javascript复制
下面直接给出BeanFactory上面的注释代码:
代码语言:javascript复制
Bean factory implementations should support the standard bean lifecycle interfacesas far as possible. The full set of initialization methods and their standard order is:
1.BeanNameAware's setBeanName
2.BeanClassLoaderAware's setBeanClassLoader
3.BeanFactoryAware's setBeanFactory
4.EnvironmentAware's setEnvironment
5.EmbeddedValueResolverAware's setEmbeddedValueResolver
6.ResourceLoaderAware's setResourceLoader(only applicable when running in an application context)
7.ApplicationEventPublisherAware's setApplicationEventPublisher(only applicable when running in an application context)
8.MessageSourceAware's setMessageSource(only applicable when running in an application context)
9.ApplicationContextAware's setApplicationContext(only applicable when running in an application context)
10.ServletContextAware's setServletContext(only applicable when running in a web application context)
11.postProcessBeforeInitialization methods of BeanPostProcessors
12.InitializingBean's afterPropertiesSet
13.a custom init-method definition
14.postProcessAfterInitialization methods of BeanPostProcessors
On shutdown of a bean factory, the following lifecycle methods apply:
1.postProcessBeforeDestruction methods of DestructionAwareBeanPostProcessors
2.DisposableBean's destroy
3.a custom destroy-method definition