LifeCycleBean
package main.com.lee.d_lifecycle; public class LifeCycleBean { public LifeCycleBean() { System.out.println("LifeCycleBean 构造了..."); } public void setUp() { System.out.println("LifeCycleBean 初始化了..."); } public void tearDown() { System.out.println("LifeCycleBean 销毁了..."); } }
applicationContext.xml
<!-- Bean生命周期 --> <bean id="lifeCycleBean" class="main.com.lee.d_lifecycle.LifeCycleBean" init-method="setUp" destroy-method="tearDown" />
Test
package main.com.lee.d_lifecycle; import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; public class LifeCycleTest { @Test public void demo1() { // ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); LifeCycleBean lifeCycleBean = (LifeCycleBean) applicationContext.getBean("lifeCycleBean"); System.out.println(lifeCycleBean); System.out.println(333); applicationContext.close(); } }
Out
LifeCycleBean 构造了... LifeCycleBean 初始化了... main.com.lee.d_lifecycle.LifeCycleBean@c791b9 333 14:03:37,594 INFO ClassPathXmlApplicationContext:1042 - Closing org.springframework.context.support.ClassPathXmlApplicationContext@efd552: startup date [Mon Dec 23 14:03:37 CST 2013]; root of context hierarchy 14:03:37,595 INFO DefaultListableBeanFactory:444 - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1b48197: defining beans [lifeCycleBean]; root of factory hierarchy LifeCycleBean 销毁了...