Spring實戰(zhàn)之Bean銷毀之前的行為操作示例
本文實例講述了Spring實戰(zhàn)之Bean銷毀之前的行為操作。分享給大家供大家參考,具體如下:
一 配置
<?xml version="1.0" encoding="GBK"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <bean id="steelAxe" class="org.crazyit.app.service.impl.SteelAxe"/> <!-- 配置chinese Bean,使用destroy-method="close" 指定該Bean實例被銷毀之前,Spring會自動執(zhí)行指定該Bean的close方法 --> <bean id="chinese" class="org.crazyit.app.service.impl.Chinese" destroy-method="close"> <property name="axe" ref="steelAxe"/> </bean> </beans>
二 接口
1 Axe
package org.crazyit.app.service; public interface Axe { public String chop(); }
2 Person
package org.crazyit.app.service; public interface Person { public void useAxe(); }
三 Bean
1 Chinese
package org.crazyit.app.service.impl; import org.springframework.beans.factory.DisposableBean; import org.crazyit.app.service.*; public class Chinese implements Person,DisposableBean { private Axe axe; public Chinese() { System.out.println("Spring實例化主調bean:Chinese實例..."); } public void setAxe(Axe axe) { System.out.println("Spring執(zhí)行依賴關系注入..."); this.axe = axe; } public void useAxe() { System.out.println(axe.chop()); } public void close() { System.out.println("正在執(zhí)行銷毀之前的方法 close..."); } public void destroy() throws Exception { System.out.println("正在執(zhí)行銷毀之前的方法 destroy..."); } }
2 SteelAxe
package org.crazyit.app.service.impl; import org.crazyit.app.service.*; public class SteelAxe implements Axe { public SteelAxe() { System.out.println("Spring實例化依賴bean:SteelAxe實例..."); } public String chop() { return "鋼斧砍柴真快"; } }
四 測試類
package lee; import org.springframework.context.*; import org.springframework.context.support.*; import org.crazyit.app.service.*; public class BeanTest { public static void main(String[] args) { // 以CLASSPATH路徑下的配置文件創(chuàng)建ApplicationContext AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 獲取容器中的Bean實例 Person p = ctx.getBean("chinese" , Person.class); p.useAxe(); // 為Spring容器注冊關閉鉤子 ctx.registerShutdownHook(); } }
五 測試結果
Spring實例化依賴bean:SteelAxe實例...
Spring實例化主調bean:Chinese實例...
Spring執(zhí)行依賴關系注入...
鋼斧砍柴真快
九月 21, 2019 9:30:18 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@5a10411: startup date [Sat Sep 21 21:30:18 CST 2019]; root of context hierarchy
正在執(zhí)行銷毀之前的方法 destroy...
正在執(zhí)行銷毀之前的方法 close...
更多關于java相關內容感興趣的讀者可查看本站專題:《Spring框架入門與進階教程》、《Java數(shù)據(jù)結構與算法教程》、《Java操作DOM節(jié)點技巧總結》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設計有所幫助。
- Spring boot將配置屬性注入到bean類中
- springboot 獲取工具類bean過程詳解
- Java類獲取Spring中bean的5種方式
- spring boot中的條件裝配bean的實現(xiàn)
- Spring實戰(zhàn)之Bean的后處理器操作示例
- Spring實戰(zhàn)之Bean定義中的SpEL表達式語言支持操作示例
- Spring實戰(zhàn)之獲取其他Bean的屬性值操作示例
- Spring實戰(zhàn)之協(xié)調作用域不同步的Bean操作示例
- Spring的自動裝配Bean的三種方式
- Spring中多配置文件及引用其他bean的方式
- Spring實戰(zhàn)之搜索Bean類操作示例
相關文章
使用dynamic datasource springboot starter實現(xiàn)多數(shù)據(jù)源及源碼分析
這篇文章主要介紹了使用dynamic-datasource-spring-boot-starter做多數(shù)據(jù)源及源碼分析,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09elasticsearch索引創(chuàng)建create?index集群matedata更新
這篇文章主要介紹了elasticsearch索引創(chuàng)建create?index及集群matedata更新,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-04-04Springboot利用Aop捕捉注解實現(xiàn)業(yè)務異步執(zhí)行
在開發(fā)過程中,盡量會將比較耗時且并不會影響請求的響應結果的業(yè)務放在異步線程池中進行處理,那么到時什么任務在執(zhí)行的時候會創(chuàng)建單獨的線程進行處理呢?這篇文章主要介紹了Springboot利用Aop捕捉注解實現(xiàn)業(yè)務異步執(zhí)行2023-04-04Java 基于UDP協(xié)議實現(xiàn)消息發(fā)送
這篇文章主要介紹了Java 基于UDP協(xié)議實現(xiàn)消息發(fā)送,幫助大家更好的理解和學習Java網(wǎng)絡編程,感興趣的朋友可以了解下2020-11-11java?集合工具類Collections及Comparable和Comparator排序詳解
這篇文章主要介紹了java集合工具類Collections及Comparable和Comparator排序詳解,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-06-06springboot FeignClient注解及參數(shù)
這篇文章主要介紹了springboot FeignClient注解及參數(shù),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-12-12