BeanFactory 中bean的生命周期

如果使用BeanFactory来生成、管理Bean, 一个Bean从建立到销毁,会历经几个执行阶段。 我个人理解一般的bean的生命周期主要包括:建立,初始化,使用阶段,销毁四个核心阶段。

1、@Bean的建立 由BeanFactory读取Bean定义文件,并生成各个Bean实例。

2、初始化@属性注入 执行相关的Bean属性依赖注入

@BeanNameAware的setBeanName() org.springframework.beans.factory.BeanNameAware

@BeanFactoryAware的setBeanFactory() org.springFramework.beans.factory.BeanFactoryAware

@BeanPostProcessors的processBeforeInitialization() org.springFramework.beans.factory.config.BeanPostProcessors

@InitializiongBean的afterPropertiesSet() org.springframework.beans.factory.InitializingBean

@Bean定义文件中定义init-method use “init-methd” to set method name e.g:

<bean id=”helloBean” class=”org.bearfly.bean.HelloBean” init-method=”initBean”>

当代码运行到这个阶段的时候,,就是执行initBean方法。

@BeanPostProcessors的processaAfterInitialization() 如果有任何的BeanPostProcessors实例与Bean实例关联,则执行BeanPostProcessor实例的processaAfterInitialization()方法。

4、销毁 @DisposableBean的destroy() 容器关闭 org.springframework.beans.factory.DisposableBean

@Bean定义文件中定义destroy-method

<bean id=”helloBean” class=”org.bearfly.bean.HelloBean” destroy-method=”destroyBean”>

为了一些琐事吵架,然后冷战,疯狂思念对方,最后和好。

BeanFactory 中bean的生命周期

相关文章:

你感兴趣的文章:

标签云: