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

歡迎訪問 生活随笔!

生活随笔

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

javascript

Spring @PostConstruct和@PreDestroy实例

發布時間:2024/9/20 javascript 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring @PostConstruct和@PreDestroy实例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在Spring中,既可以實現InitializingBean和DisposableBean接口或在bean配置文件中指定?init-method?和?destroy-method?在初始化和銷毀回調函數。在這篇文章中,我們將介紹如何使用 @PostConstruct 和 @PreDestroy?注解來做同樣的事情。

注:@PostConstruct和@PreDestroy 標注不屬于 Spring,它是在J2EE庫-?common-annotations.jar。

@PostConstruct 和 @PreDestroy

一個 CustomerService?Bean使用 @PostConstruct 和 @PreDestroy 注釋 package com.yiibai.customer.services;import javax.annotation.PostConstruct; import javax.annotation.PreDestroy;public class CustomerService {String message;public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}@PostConstructpublic void initIt() throws Exception {System.out.println("Init method after properties are set : " + message);}@PreDestroypublic void cleanUp() throws Exception {System.out.println("Spring Container is destroy! Customer clean up");}} 默認情況下,Spring不會意識到@PostConstruct和@PreDestroy注解。要啟用它,要么注冊“CommonAnnotationBeanPostProcessor”,要么在bean配置文件的<context:annotation-config?/>‘?指定,

1. CommonAnnotationBeanPostProcessor

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" /><bean id="customerService" class="com.yiibai.customer.services.CustomerService"><property name="message" value="i'm property message" /></bean></beans>

2. <context:annotation-config />

<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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd"><context:annotation-config /><bean id="customerService" class="com.yiibai.customer.services.CustomerService"><property name="message" value="i'm property message" /></bean></beans>

執行結果

package com.yiibai.common;import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;import com.yiibai.customer.services.CustomerService;public class App {public static void main( String[] args ){ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"});CustomerService cust = (CustomerService)context.getBean("customerService");System.out.println(cust);context.close();} }

輸出結果

Init method after properties are set : im property message com.yiibai.customer.services.CustomerService@47393f ... INFO: Destroying singletons in org.springframework.beans.factory. support.DefaultListableBeanFactory@77158a: defining beans [customerService]; root of factory hierarchy Spring Container is destroy! Customer clean up initIt()方法(@PostConstruct)被調用時,消息屬性設置后?cleanUp()?方法(@PreDestroy)是在context.close()執行后被調用; 下載源代碼 –?http://pan.baidu.com/s/1qX2W6xI

總結

以上是生活随笔為你收集整理的Spring @PostConstruct和@PreDestroy实例的全部內容,希望文章能夠幫你解決所遇到的問題。

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