日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring容器初始化Bean、销毁Bean前所做操作的定义方式汇总

發布時間:2025/4/16 javascript 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring容器初始化Bean、销毁Bean前所做操作的定义方式汇总 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、通過@javax.annotation.PostConstruct和@javax.annotation.PreDestroy定義

package com.xiaochuange.platform.spring4;import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy;/*** @Desc:* @Auther: spring* @Date: 2018-9-12 14:56*/ @Component("initAndDestoryService") public class InitAndDestoryService {// 銷毀之前執行@PreDestroypublic void methodPreDestroy() {System.out.println("InitAndDestoryService @PreDestroy invoke...");}// 初始化之前執行@PostConstructpublic void methodPostConstruct() {System.out.println("InitAndDestoryService @PostConstruct invoke...");}}

2、通過xml配置文件指定init-method和destroy-method

package com.xiaochuange.platform.spring4;/*** @Desc:* @Auther: spring* @Date: 2018-9-12 15:03*/ public class XmlInitAndDestoryService {private String message;public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}public void methodInit(){System.out.println("XmlInitAndDestoryService methodInit invoke..." + message);}public void methodDestory(){System.out.println("XmlInitAndDestoryService methodDestory invoke..." + message);}}

applicationContext.xml

<bean id="xmlInitAndDestoryService"class="com.xiaochuange.platform.spring4.XmlInitAndDestoryService"init-method="methodInit" destroy-method="methodDestory"><property name="message" value="234"></property> </bean>

3、通過bean實現InitializingBean和DisposableBean接口

package com.xiaochuange.platform.spring4;import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean;/*** @Desc:* @Auther: spring* @Date: 2018-9-12 14:27*/ public class InitializingBeanAndDisposableBeanService implements InitializingBean, DisposableBean {private String message;public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}@Overridepublic void destroy() throws Exception {// 銷毀時的操作System.out.println("InitializingBeanAndDisposableBeanService destroy() invoke..." + message);}@Overridepublic void afterPropertiesSet() throws Exception {// 初始化時的操作System.out.println("InitializingBeanAndDisposableBeanService afterPropertiesSet() invoke..." + message);}}

applicationContext.xml

<bean id="personService" class="com.xiaochuange.platform.spring4.InitializingBeanAndDisposableBeanService"><property name="message" value="123"></property> </bean>

4、測試

package com.xiaochuange.platform.spring4;import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;/*** @Desc:* @Auther: spring* @Date: 2018-9-12 14:45*/ public class MainTest {public static void main(String[] args) {AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");InitializingBeanAndDisposableBeanService personService = (InitializingBeanAndDisposableBeanService) context.getBean("personService");InitAndDestoryService initAndDestoryService = (InitAndDestoryService) context.getBean("initAndDestoryService");XmlInitAndDestoryService xmlInitAndDestoryService = (XmlInitAndDestoryService) context.getBean("xmlInitAndDestoryService");context.registerShutdownHook();// 輸出如下: // InitAndDestoryService @PostConstruct invoke... // InitializingBeanAndDisposableBeanService afterPropertiesSet() invoke...123 // XmlInitAndDestoryService methodInit invoke...234 // XmlInitAndDestoryService methodDestory invoke...234 // InitializingBeanAndDisposableBeanService destroy() invoke...123 // InitAndDestoryService @PreDestroy invoke...} }

轉載于:https://blog.51cto.com/springsupervip/2174254

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的Spring容器初始化Bean、销毁Bean前所做操作的定义方式汇总的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。