关键字: lazy-init,init-method,destroy-method,depends-on的说明
lazy-init,init-method,destroy-method,depends-on的说明(这4个是Spring 1就有的)
- lazy-init是否延迟初始化
- init-method指定初始化方法
- destroy-method指定销毁方法
- depends-on指定依赖的bean(指定bean的初始化顺序)
- <bean id="bean11" class="test.impl.Bean11" lazy-init="true" init-method="init" destroy-method="destroy" depends-on="bean1"/>
- <bean id="bean12" class="test.impl.Bean12" lazy-init="false" init-method="init" destroy-method="destroy" depends-on="bean2"/>
- <bean id="bean1" class="test.impl.Bean1" init-method="init" destroy-method="destroy" lazy-init="true"/>
- <bean id="bean2" class="test.impl.Bean2" init-method="init" destroy-method="destroy" />
<bean id="bean11" class="test.impl.Bean11" lazy-init="true" init-method="init" destroy-method="destroy" depends-on="bean1"/>
<bean id="bean12" class="test.impl.Bean12" lazy-init="false" init-method="init" destroy-method="destroy" depends-on="bean2"/>
<bean id="bean1" class="test.impl.Bean1" init-method="init" destroy-method="destroy" lazy-init="true"/>
<bean id="bean2" class="test.impl.Bean2" init-method="init" destroy-method="destroy" />
说明:
- 一个bean的生命周期是constructor->init->destroy
- lazy-init决定了bean的初始化时机 (bean1和bean11在被调用时,bean2和bean12在ApplicationContext初始化时)
- depends-on决定了beans的初始化顺序(bean11在bean1初始化后,bean22在bean2初始化后)和销毁顺序(bean11在bean1销毁前,bean22在bean2销毁前)