Spring Init Destory

来源:互联网 发布:java流程图 编辑:程序博客网 时间:2024/04/28 12:16
    <bean            id="initDestroy"            class="com.sanmao.spring.ioc.InitDestroy"            init-method="init"            destroy-method="destroy"></bean>
public class testInitDestory {    /**     * 1.启动spring 容器     * 2.创建对象     * 3.执行init 方法,由spring 内部调用的     * 4.context.getBean把对象提取出来     * 5.对象调用方法     * 6.当执行close方法的时候,调用销毁方法     *     * 说明:设置多实例,不负责销毁     * */    @Test    public void testinitdestory(){        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");        InitDestroy initDestroy=(InitDestroy) context.getBean("initDestroy");        initDestroy.sayHello();        //用下面的方式关闭spring 容器        ClassPathXmlApplicationContext applicationContext=(ClassPathXmlApplicationContext)context;        applicationContext.close();    }}
package com.sanmao.spring.ioc;public class InitDestroy {    public  InitDestroy(){        System.out.println("spring 在默认的情况下,使用默认的构造函数");    }    public void sayHello(){        System.out.println("hello");    }    public void init(){        System.out.println("init");    }    public void destroy(){        System.out.println("destroy");    }}
0 0
原创粉丝点击