Spring init-method與destroy-method屬性的用法解析
Spring init-method與destroy-method屬性使用
知識(shí)點(diǎn)介紹:
有時(shí)候在bean初始化之后要執(zhí)行的初始化方法,以及在bean銷毀時(shí)執(zhí)行的方法。這時(shí)就需要配置init-method和destroy-method屬性,顧名思義,配置初始與銷毀的方法。
操作步驟:
1、創(chuàng)建Speaker對(duì)象
public class Speaker { private String name; private String topic; private Speaker(String name,String topic){ this.name = name; this.topic = topic; } /** * Speaker實(shí)例化時(shí)執(zhí)行的方法 */ private void init() { System.out.println("執(zhí)行Speaker 的 初始化方法 init"); } /** * Speaker銷毀時(shí)執(zhí)行的方法 */ private void destroy() { System.out.println("執(zhí)行Speaker 的銷毀方法 destroy"); } public void teach() { System.out.println(toString()); } @Override public String toString() { return "Speaker [name=" + name + ", topic=" + topic + "]"; } }
2、創(chuàng)建Spring配置文件beanLearn05.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- Learn 05 init-method和destroy-method屬性的使用 --> <bean id="speaker05" class="com.mahaochen.spring.learn05.Speaker" init-method="init" destroy-method="destroy"> <constructor-arg index="0" value="elle" /> <constructor-arg index="1" value="Study Hard!" /> </bean> </beans>
3、將Spring配置文件beanLearn05.xml引入到主配置文件beans.xml中。
<!-- Learn 04 使用實(shí)例工廠方式實(shí)例化Bean --> <import resource="com/mahaochen/spring/learn05/beanLearn05.xml"/>
4、編寫(xiě)測(cè)試類TestSpring05.java。
public class TestSpring05 { public static void main(String[] args) { ApplicationContext appContext = new ClassPathXmlApplicationContext("beans.xml"); Speaker speaker05 = (Speaker) appContext.getBean("speaker05"); speaker05.teach(); ((ClassPathXmlApplicationContext) appContext).close(); } }
init-method="init"和 destroy-method="close" 作用
一般在我們配置數(shù)據(jù)源的時(shí)候,會(huì)這樣寫(xiě)
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
init-method="init" destroy-method="close" 作用:
init-method="init"
是指bean被初始化時(shí)執(zhí)行的方法,當(dāng)bean實(shí)例化后,執(zhí)行init-method用于初始化數(shù)據(jù)庫(kù)連接池。
destroy-method="close"
是指bean被銷毀時(shí)執(zhí)行的方法 Spring容器關(guān)閉時(shí)調(diào)用該方法即調(diào)用close()將連接關(guān)閉。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)MD5加密及解密的代碼實(shí)例分享
如果對(duì)安全性的需求不是太高,MD5仍是使用非常方便和普及的加密方式,比如Java中自帶的MessageDigest類就提供了支持,這里就為大家?guī)?lái)Java實(shí)現(xiàn)MD5加密及解密的代碼實(shí)例分享:2016-06-06Spring Security 單點(diǎn)登錄簡(jiǎn)單示例詳解
這篇文章主要介紹了Spring Security 單點(diǎn)登錄簡(jiǎn)單示例詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-02-02springboot中rabbitmq實(shí)現(xiàn)消息可靠性機(jī)制詳解
這篇文章主要介紹了springboot中rabbitmq實(shí)現(xiàn)消息可靠性機(jī)制詳解,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-09-09Spring?Boot中KafkaListener的介紹、原理和使用方法案例詳解
本文介紹了Spring Boot中 @KafkaListener 注解的介紹、原理和使用方法,通過(guò)本文的介紹,我們希望讀者能夠更好地理解Spring Boot中 @KafkaListener 注解的使用方法,并在項(xiàng)目中更加靈活地應(yīng)用2023-09-09Java幾個(gè)實(shí)例帶你進(jìn)階升華下篇
與其明天開(kāi)始,不如現(xiàn)在行動(dòng),本文為你帶來(lái)幾個(gè)Java書(shū)寫(xiě)的實(shí)際案例,對(duì)鞏固編程的基礎(chǔ)能力很有幫助,快來(lái)一起往下看看吧2022-03-03Java組件commons fileupload實(shí)現(xiàn)文件上傳功能
這篇文章主要為大家詳細(xì)介紹了Java組件commons fileupload實(shí)現(xiàn)文件上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10