Spring Init Destory

    xiaoxiao2023-03-24  3

    <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"); } }
    转载请注明原文地址: https://ju.6miu.com/read-1201866.html
    最新回复(0)