Spring實(shí)戰(zhàn)之使用@POSTConstruct和@PreDestroy定制生命周期行為操作示例
本文實(shí)例講述了Spring實(shí)戰(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">
<!-- 自動(dòng)掃描指定包及其子包下的所有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;
// 實(shí)現(xiàn)Person接口的useAxe()方法
public void useAxe()
{
// 調(diào)用axe的chop()方法,
// 表明Person對(duì)象依賴于axe對(duì)象
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 "石斧砍柴好慢";
}
}
四 測(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)
{
// 創(chuàng)建Spring容器
AbstractApplicationContext ctx = new
ClassPathXmlApplicationContext("beans.xml");
// 注冊(cè)關(guān)閉鉤子
ctx.registerShutdownHook();
Person person = ctx.getBean("chinese" , Person.class);
person.useAxe();
}
}
五 測(cè)試結(jié)果
正在執(zhí)行初始化的init方法...
鋼斧砍柴真快
正在執(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ì)有所幫助。
相關(guān)文章
Springboot工具類ReflectionUtils使用教程
這篇文章主要介紹了Springboot內(nèi)置的工具類之ReflectionUtils的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-12-12
Spring Aop之AspectJ注解配置實(shí)現(xiàn)日志管理的方法
下面小編就為大家分享一篇Spring Aop之AspectJ注解配置實(shí)現(xiàn)日志管理的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Java實(shí)現(xiàn)生成二維碼展示到瀏覽器的示例代碼
這篇文章主要介紹了Java實(shí)現(xiàn)生成二維碼展示到瀏覽器的示例代碼,要實(shí)現(xiàn)在瀏覽器展示二維碼,那么首先需要html文件,通過(guò)Java生成二維碼的工具類,在controller層調(diào)用接口,就可以實(shí)現(xiàn)在瀏覽器上展示二維碼,需要的朋友可以參考下2024-01-01
Java數(shù)據(jù)結(jié)構(gòu)之單鏈表的實(shí)現(xiàn)與面試題匯總
由于順序表的插入刪除操作需要移動(dòng)大量的元素,影響了運(yùn)行效率,因此引入了線性表的鏈?zhǔn)酱鎯?chǔ)——單鏈表。本文為大家介紹了單鏈表的實(shí)現(xiàn)與面試題匯總,感興趣的可以了解一下2022-10-10
@Valid 無(wú)法校驗(yàn)List<E>的問(wèn)題
這篇文章主要介紹了@Valid 無(wú)法校驗(yàn)List<E>的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
Spring?Boot?接口加解密功能實(shí)現(xiàn)
在我們?nèi)粘5腏ava開(kāi)發(fā)中,免不了和其他系統(tǒng)的業(yè)務(wù)交互,或者微服務(wù)之間的接口調(diào)用;如果我們想保證數(shù)據(jù)傳輸?shù)陌踩?,?duì)接口出參加密,入?yún)⒔饷?,這篇文章主要介紹了Spring?Boot?接口加解密功能實(shí)現(xiàn),需要的朋友可以參考下2023-04-04
Java Swing JPasswordField密碼框的實(shí)現(xiàn)示例
這篇文章主要介紹了Java Swing JPasswordField密碼框的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Java快速實(shí)現(xiàn)PDF轉(zhuǎn)圖片功能實(shí)例代碼
PDFBox是一個(gè)開(kāi)源Java類庫(kù),用于讀取和創(chuàng)建PDF文檔,它支持文本提取、表單處理、文檔加密解密、合并分割、內(nèi)容覆蓋追加、文檔打印和轉(zhuǎn)換等功能,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-09-09
詳解Spring boot使用Redis集群替換mybatis二級(jí)緩存
本篇文章主要介紹了詳解Spring boot使用Redis集群替換mybatis二級(jí)緩存,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05

