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中Mybatis注解一對多和多對多查詢實現(xiàn)示例
這篇文章主要介紹了SpringBoot中Mybatis注解一對多和多對多查詢的實現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-03-03JavaMap兩種遍歷方式keySet與entrySet詳解
這篇文章主要介紹了JavaMap兩種遍歷方式keySet與entrySet,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-03-03Java的SpringMVC中控制器返回XML數(shù)據(jù)問題
這篇文章主要介紹了Java的SpringMVC中控制器返回XML數(shù)據(jù)問題,控制器是處理HTTP請求的組件,它們接收來自客戶端的請求,并將其轉(zhuǎn)換為適當?shù)捻憫?yīng),這些響應(yīng)可以是動態(tài)生成的?HTML?頁面,也可以是JSON或XML格式的數(shù)據(jù),需要的朋友可以參考下2023-07-07詳解MyBatis Mapper 代理實現(xiàn)數(shù)據(jù)庫調(diào)用原理
這篇文章主要介紹了詳解MyBatis Mapper 代理實現(xiàn)數(shù)據(jù)庫調(diào)用原理,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10