1. 程式人生 > >spring 中bean的作用域和生命週期

spring 中bean的作用域和生命週期

spring中bean的作用域:


生命週期(官方圖):

spring 中bean例項化過程(不好意思,我盜圖了):


1:instantiate bean物件例項化
2:populate properties 封裝屬性
3:如果Bean實現BeanNameAware 執行 setBeanName
4:如果Bean實現BeanFactoryAware 或者 ApplicationContextAware 設定工廠 setBeanFactory 或者上下文物件 setApplicationContext
5:如果存在類實現 BeanPostProcessor(後處理Bean) ,執行postProcessBeforeInitialization
6:如果Bean實現InitializingBean 執行 afterPropertiesSet 
7:呼叫<bean init-method="init"> 指定初始化方法 init
8:如果存在類實現 BeanPostProcessor(處理Bean) ,執行postProcessAfterInitialization
9:執行業務處理
10:如果Bean實現 DisposableBean 執行 destroy
11:呼叫<bean destroy-method="customerDestroy"> 指定銷燬方法 customerDestroy