Spring中Bean的生命周期
建议看篇文章Bean的生命周期
我写个大概的流程:
- 实例化:通过反射实例化Bean的实例;
- 属性注入:为Bean设置相应的属性值;
- 初始化:
- 如果Bean实现了xxxAware接口,会调用相应的方法;
- 如果有相关的BeanPostProcessor对象,会调用postProcessBeforeInitialization()方法;
- 如果Bean实现了InitializingBean接口,调用afterPropertiesSet()方法;
- 如果Bean定义了init-method属性指定初始化方法,调用相应的方法。
- 使用:这时候Bean就可以被使用了;
- 销毁:
- 如果Bean实现了DisposableBean接口,调用destroy()方法;
- 如果Bean定义了destroy-method属性指定销毁方法,调用相应的方法。