最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

spring?bean標(biāo)簽中的init-method和destroy-method詳解

 更新時(shí)間:2023年04月15日 11:38:58   作者:amcomputer  
這篇文章主要介紹了spring?bean標(biāo)簽中的init-method和destroy-method,在很多項(xiàng)目中,經(jīng)常在xml配置文件中看到init-method 或者 destroy-method ,因此整理收集下,方便以后參考和學(xué)習(xí),需要的朋友可以參考下

1 背景介紹

在很多項(xiàng)目中,經(jīng)常在xml配置文件中看到init-method 或者 destroy-method 。因此整理收集下,方便以后參考和學(xué)習(xí)??梢允褂?init-method 和 destroy-method 在bean 配置文件屬性用于在bean初始化和銷毀某些動作時(shí)。這是用來替代 InitializingBean和DisposableBean接口。

init-method 用于指定bean的初始化方法。 spring 容器會幫我們實(shí)例化對象,實(shí)例化對象之后,spring就會查找我們是否配置了init-method。如果在標(biāo)簽配置了init-method,spring就會調(diào)用我們配置的init-method 方法,進(jìn)行bean的初始化。需要注意的是,構(gòu)建方法先執(zhí)行,執(zhí)行完后就會執(zhí)行 init-method 。

2 init-method

xml配置

    <bean id="testService" class="com.test.TestService" init-method="myInit" destroy-method="myDestroy">
    </bean>
public class TestService {

    public TestService(){
        System.out.println("實(shí)例化:TestService");
    }

    public void myInit(){
        System.out.println("初始化:TestService");
    }

    public void myDestroy(){
        System.out.println("銷毀:TestService");
    }
}

測試

public class App 
{
    public static void main( String[] args )
    {
    	ConfigurableApplicationContext context = 
		new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});
	
    	TestService cust = (CustomerService)context.getBean("testService");
    	
    	System.out.println("hhhhh");
    	
    	//context.close();
    }
}

輸出:

實(shí)例化:TestService
初始化:TestService
hhhhh

3 destroy-method

public class App 
{
    public static void main( String[] args )
    {
    	ConfigurableApplicationContext context = 
		new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});
	
    	TestService cust = (CustomerService)context.getBean("testService");
    	
    	System.out.println("hhhhh");
    	
    	context.close();
    }
}

spring上下文關(guān)閉時(shí)候,才會進(jìn)行銷毀。

輸出:

實(shí)例化:TestService
初始化:TestService
hhhhh
銷毀:TestService

4 總結(jié)

建議使用init-method 和 destroy-methodbean 在Bena配置文件,而不是執(zhí)行 InitializingBean 和 DisposableBean 接口,也會造成不必要的耦合代碼在Spring。

到此這篇關(guān)于spring bean標(biāo)簽中的init-method和destroy-method的文章就介紹到這了,更多相關(guān)spring  init-method和destroy-method內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

九寨沟县| 吉首市| 临西县| 开封市| 呼玛县| 伊金霍洛旗| 昌平区| 静海县| 台中市| 蓬安县| 丰镇市| 六枝特区| 沙洋县| 内丘县| 南澳县| 赣榆县| 象州县| 阳城县| 新安县| 平昌县| 玉田县| 贺州市| 西吉县| 丹阳市| 阳朔县| 调兵山市| 驻马店市| 尖扎县| 新余市| 顺义区| 井冈山市| 民勤县| 饶阳县| 介休市| 玉田县| 浦江县| 巴中市| 冷水江市| 北海市| 琼海市| 柘荣县|