Spring實(shí)戰(zhàn)之Bean銷毀之前的行為操作示例
本文實(shí)例講述了Spring實(shí)戰(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實(shí)例被銷毀之前,Spring會(huì)自動(dòng)執(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實(shí)例化主調(diào)bean:Chinese實(shí)例...");
}
public void setAxe(Axe axe)
{
System.out.println("Spring執(zhí)行依賴關(guān)系注入...");
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實(shí)例化依賴bean:SteelAxe實(shí)例...");
}
public String chop()
{
return "鋼斧砍柴真快";
}
}
四 測(cè)試類
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實(shí)例
Person p = ctx.getBean("chinese" , Person.class);
p.useAxe();
// 為Spring容器注冊(cè)關(guān)閉鉤子
ctx.registerShutdownHook();
}
}
五 測(cè)試結(jié)果
Spring實(shí)例化依賴bean:SteelAxe實(shí)例...
Spring實(shí)例化主調(diào)bean:Chinese實(shí)例...
Spring執(zhí)行依賴關(guān)系注入...
鋼斧砍柴真快
九月 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...
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
- Spring boot將配置屬性注入到bean類中
- springboot 獲取工具類bean過程詳解
- Java類獲取Spring中bean的5種方式
- spring boot中的條件裝配bean的實(shí)現(xiàn)
- Spring實(shí)戰(zhàn)之Bean的后處理器操作示例
- Spring實(shí)戰(zhàn)之Bean定義中的SpEL表達(dá)式語(yǔ)言支持操作示例
- Spring實(shí)戰(zhàn)之獲取其他Bean的屬性值操作示例
- Spring實(shí)戰(zhàn)之協(xié)調(diào)作用域不同步的Bean操作示例
- Spring的自動(dòng)裝配Bean的三種方式
- Spring中多配置文件及引用其他bean的方式
- Spring實(shí)戰(zhàn)之搜索Bean類操作示例
相關(guān)文章
使用nexus3.X上傳本地jar包并且通過pom讀取的解決方案(全網(wǎng)最新)
這篇文章主要介紹了使用nexus3.X上傳本地jar包并且通過pom讀取的解決方案(全網(wǎng)最新),本文內(nèi)容有點(diǎn)長(zhǎng),結(jié)合圖文實(shí)例給大家講解的非常詳細(xì),需要的朋友可以參考下2023-11-11
合并有序數(shù)組的實(shí)現(xiàn)(java與C語(yǔ)言)
這篇文章主要介紹了合并有序數(shù)組的實(shí)現(xiàn)(java與C語(yǔ)言)的相關(guān)資料,這里對(duì)有序數(shù)組的合并分享了java版本和C語(yǔ)言版本的示例,需要的朋友可以參考下2017-08-08
SpringBoot后端服務(wù)重定向的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot后端服務(wù)重定向的實(shí)現(xiàn)示例,通過重定向、路徑匹配、反向代理和直接調(diào)用Controller層接口等方法來實(shí)現(xiàn),感興趣的可以了解一下2025-01-01
Spring框架 引入@Resource注解報(bào)空指針的解決
這篇文章主要介紹了Spring框架 引入@Resource注解報(bào)空指針的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
簡(jiǎn)述Java編程語(yǔ)言對(duì)象的容納
這篇文章主要對(duì)Java編程思想中對(duì)象的容納進(jìn)行了一個(gè)總體的介紹,具有一定的參考價(jià)值,需要的朋友可以了解下。2017-09-09
Springboot整合實(shí)現(xiàn)郵件發(fā)送的原理詳解
SpringBoot集成郵件服務(wù)非常簡(jiǎn)單,通過簡(jiǎn)單的學(xué)習(xí)即可快速掌握郵件業(yè)務(wù)類的核心邏輯和企業(yè)郵件的日常服務(wù),本文給大家分享Springboot整合實(shí)現(xiàn)郵件發(fā)送的原理,一起看看吧2021-06-06
springboot打包不同環(huán)境配置以及shell腳本部署的方法
這篇文章主要給大家介紹了關(guān)于springboot打包不同環(huán)境配置以及shell腳本部署的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用springboot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
java 根據(jù)經(jīng)緯度獲取地址實(shí)現(xiàn)代碼
這篇文章主要介紹了 java 根據(jù)經(jīng)緯度獲取地址實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-05-05

