Spring實戰(zhàn)之使用@POSTConstruct和@PreDestroy定制生命周期行為操作示例
本文實例講述了Spring實戰(zhàn)之使用@POSTConstruct和@PreDestroy定制生命周期行為操作。分享給大家供大家參考,具體如下:
一 配置
<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 自動掃描指定包及其子包下的所有Bean類 -->
<context:component-scan
base-package="org.crazyit.app.service"/>
</beans>
二 接口
Axe
package org.crazyit.app.service;
public interface Axe
{
public String chop();
}
Person
package org.crazyit.app.service;
public interface Person
{
public void useAxe();
}
三 Bean
Chinese
package org.crazyit.app.service.impl;
import org.springframework.stereotype.*;
import javax.annotation.*;
import org.crazyit.app.service.*;
@Component
public class Chinese implements Person
{
// 執(zhí)行Field注入
@Resource(name="steelAxe")
private Axe axe;
// 實現(xiàn)Person接口的useAxe()方法
public void useAxe()
{
// 調(diào)用axe的chop()方法,
// 表明Person對象依賴于axe對象
System.out.println(axe.chop());
}
@PostConstruct
public void init()
{
System.out.println("正在執(zhí)行初始化的init方法...");
}
@PreDestroy
public void close()
{
System.out.println("正在執(zhí)行銷毀之前的close方法...");
}
}
SteelAxe
package org.crazyit.app.service.impl;
import org.springframework.stereotype.*;
import org.crazyit.app.service.*;
@Component
public class SteelAxe implements Axe
{
public String chop()
{
return "鋼斧砍柴真快";
}
}
StoneAxe
package org.crazyit.app.service.impl;
import org.springframework.stereotype.*;
import org.crazyit.app.service.*;
@Component
public class StoneAxe implements Axe
{
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)
{
// 創(chuàng)建Spring容器
AbstractApplicationContext ctx = new
ClassPathXmlApplicationContext("beans.xml");
// 注冊關(guān)閉鉤子
ctx.registerShutdownHook();
Person person = ctx.getBean("chinese" , Person.class);
person.useAxe();
}
}
五 測試結(jié)果
正在執(zhí)行初始化的init方法...
鋼斧砍柴真快
正在執(zhí)行銷毀之前的close方法...
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計有所幫助。
相關(guān)文章
Springboot工具類ReflectionUtils使用教程
這篇文章主要介紹了Springboot內(nèi)置的工具類之ReflectionUtils的使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-12-12
Spring Aop之AspectJ注解配置實現(xiàn)日志管理的方法
下面小編就為大家分享一篇Spring Aop之AspectJ注解配置實現(xiàn)日志管理的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
Java數(shù)據(jù)結(jié)構(gòu)之單鏈表的實現(xiàn)與面試題匯總
由于順序表的插入刪除操作需要移動大量的元素,影響了運行效率,因此引入了線性表的鏈?zhǔn)酱鎯Α獑捂湵怼1疚臑榇蠹医榻B了單鏈表的實現(xiàn)與面試題匯總,感興趣的可以了解一下2022-10-10
Java Swing JPasswordField密碼框的實現(xiàn)示例
這篇文章主要介紹了Java Swing JPasswordField密碼框的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Java快速實現(xiàn)PDF轉(zhuǎn)圖片功能實例代碼
PDFBox是一個開源Java類庫,用于讀取和創(chuàng)建PDF文檔,它支持文本提取、表單處理、文檔加密解密、合并分割、內(nèi)容覆蓋追加、文檔打印和轉(zhuǎn)換等功能,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-09-09
詳解Spring boot使用Redis集群替換mybatis二級緩存
本篇文章主要介紹了詳解Spring boot使用Redis集群替換mybatis二級緩存,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05

