Spring中Bean的生命周期包含哪些阶段呢?首先来看看官方给出的答案。下面是Spring源码中BeanFactory接口内的一段注释。
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:(下面标红字体为较为重要的部分)
- BeanNameAware’s {@code setBeanName}
- BeanClassLoaderAware’s {@code setBeanClassLoader}
- BeanFactoryAware’s {@code setBeanFactory}
- EnvironmentAware’s {@code setEnvironment}
- EmbeddedValueResolverAware’s {@code setEmbeddedValueResolver}
- ResourceLoaderAware’s {@code setResourceLoader}
- ApplicationEventPublisherAware’s {@code setApplicationEventPublisher}
(only applicable when running in an application context)- MessageSourceAware’s {@code setMessageSource}
(only applicable when running in an application context)- ApplicationContextAware’s {@code setApplicationContext}
(only applicable when running in an application context)- ServletContextAware’s {@code setServletContext}
(only applicable when running in a web application context)- {@code postProcessBeforeInitialization} methods of BeanPostProcessors
- InitializingBean’s {@code afterPropertiesSet}
- a custom init-method definition
- {@code postProcessAfterInitialization} methods of BeanPostProcessors
可以看到1~10步中,是一系列的Aware接口。第11步执行postProcessBeforeInitialization()
,第12步执行Bean的初始化和afterPropertiesSet()
,第13步执行定义好的init-method
,第14步执行postProcessAfterInitialization()
。
Bean生命周期的整体流程如下图:
1. 实例化Bean
对于BeanFactory
容器来说,因为它的懒加载策略,所以当用户向容器请求一个尚未初始化的bean,或者初始化bean的时候需要注入另外一个尚未初始化的依赖时,容器才会调用createBean()
方法来进行Bean的实例化。
对于ApplicationContext
容器来说,在容器启动结束后,通过获取BeanDefination
对象中的信息,实例化所有的Bean。
ApplicationContext是实现了BeanFactory接口的另一个针对Web开发而产生的具有更多Spring定制功能的容器。比如,国际化、资源加载(Resource Loader)
2. 依赖注入(属性填充)
实例化后的对象被封装在BeanWrapper
对象中,接下来,Spring根据BeanDefination
中的信息以及通过BeanWrapper
提供的设置属性的接口来完成依赖注入。
3. 处理Aware接口
在完成依赖注入后,Spring会检测该对象是否实现了Aware接口的子接口,并将相关的Aware子接口实例注入给Bean。如果当前Bean对象实现了某个Aware的子接口,则会调用其内部对应的set方法。set方法是通过invokeAwareMethods()
和invokeAwareInterfaces()
由容器自动调用。比如:
- 如果这个Bean实现了
BeanNameAware
接口,会调用它实现的setBeanName()
,传入的参数是Spring配置文件中的这个Bean的id值。 - 如果这个Bean实现了
BeanFactoryAware
接口,会调用它实现的setBeanFactory()
方法,传入的参数是Spring工厂自身。 - 如果这个Bean实现了
ApplicationContextAware
,会调用它实现的setApplicationContext()
方法,传入Spring上下文
按理说,到这一步Bean已经是可以使用的对象了。但是Spring需要考虑其扩展性,因此又多了下面几个步骤。
4. postProcessorBeforeInitialization
如果想对Bean进行一些自定义的处理,那么可以让这个Bean实现BeanPostProcessor
接口,然后调用其中的postProcessBeforeInitialization()
方法。
5. InitializingBean和init-method
如果Bean实现了InitializingBean
接口,可以用afterPropertiesSet
方法做一些属性被设定后的自定义事件。然后,初始化Bean。如果Spring的配置文件中为这个Bean配置了init-method
方法,此时这个方法就会被调用。
6. postProcessorAfterInitialization
如果Bean实现了BeanPostProcessor
接口,在上一步初始化Bean完成后就会调用postProcessorAfterInitialization()
方法。因为这个方法是在Bean的初始化完成之后调用的,因此可以被应用于内存或缓存技术。
上面的步骤完成之后,Bean已经被成功创建且可以使用了。
7. DisposableBean
当Bean不再被需要时,会被清理。如果Bean实现了DisposableBean
接口,会调用其中的destory()
方法
8. destory-method
最后,如果Bean的Spring中配置了destroy-method
属性,会自动调用其配置的销毁方法。