Spring - Bean生命週期

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 銷燬了...


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章