Spring 中bean生命周期

bean 生命周期

1.Spring对bean进行初始化.
2.spring将值和对bean的引用注入到对应的属性中.
3.如果bean 实现了BeanNameAware接口,Spring将bean的ID传入到setBeanName()方法.
4如果bean实现了BeanFactoryAware接口,Spring 将调用setBeanFactory()方法,将BeanFactory容器实例传入.
5.如果Bean 实现了ApplicationContextAware接口,spring将调用setApplicationContext()方法,将bean所在应用的上下文引用传进来.
6.如果bean实现了BeanPostProcessor 接口,Spring 将调用他们的postProcessBeforeInitialization()方法.
7.如果bean实现了InitializingBean 接口,Spring 将调用他们的AfterPropertiesSet()方法.类似的如果Bean使用Init-method 声明了初始化方法,该方法也会被调用.
8.如果bean实现了BeanPostProcessor接口,Spring将调用他们的post-ProcessAfterInitialization()方法.
9.此时,bean已经准备就绪可以使用,他们将一直驻留在应用上下文中,直到销毁.
10.如果Bean实现了DisposableBean接口,Spring将调用它的 destroy()接口方法.同样如果Bean使用了destroy-method声明了销毁方法,也会被调用.

猜你喜欢

转载自blog.csdn.net/weixin_42911069/article/details/81980290