java代碼中init method和destroy method的三種使用方式

來源:互聯網
上載者:User

標籤:asc   imp   view   ext   conf   而不是   周末   最簡   通過   

在java的實際開發過程中,我們可能常常需要使用到init method和destroy method,比如初始化一個對象(bean)後立即初始化(載入)一些資料,在銷毀一個對象之前進行記憶體回收等等。
周末對這兩個方法進行了一點學習和整理,倒也不是專門為了這兩個方法,而是在鞏固spring相關知識的時候提到了,然後感覺自己並不是很熟悉這個,便好好的瞭解一下。
根據特意的去瞭解後,發現實際上可以有三種方式來實現init method和destroy method。
要用這兩個方法,自然先要知道這兩個方法究竟是幹嘛用的。而從字面意思就很容易理解,一個是載入,一個是銷毀。
下邊就正式代碼示範三種建立方式:
一、@Bean註解方式:
首先要建立一個至少擁有兩個方法的類,一個方法充當init method,另一個充當destroy method。

package springTest2;public class Test1 {    public void init() {        System.out.println("this is init method1");    }    public Test1() {        super();        System.out.println("建構函式1");    }    public void destroy() {        System.out.println("this is destroy method1");    }}

這裡很顯然只是一個普通的java類,擁有一個無參構造和另外兩個方法。
需要注意的是,這裡的init和destroy兩個方法名實際上是可以隨意取得,不叫這個也沒有問題,只不過算是一種約定俗稱,一般都是這樣叫。
另外我們也知道,這個構造方法也是可以不要的,因為會隱式的自動建立,但是為了更清楚的看到init和destroy是什麼時候執行,我們就顯示的寫出來。
建立好了這個類,我們就可以使用@Bean註解的方式指定兩個方法,以讓他們生效。

package springTest2;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@ComponentScan("springTest2")public class ConfigTest {    @Bean(initMethod = "init", destroyMethod = "destroy")    Test1 test1() {        return new Test1();    }}

這裡邊的@Configguration註解是告訴spring這個類是一個配置類,相當於我們的xml檔案,@ComponentScan則是指定需要spring來掃描的包,相當於xml中的context:component-scan屬性。
而@Bean後邊的initMethod和destroyMethod就是在聲明這是一個baen的同時指定了init和destroy方法,方法名從功能實現上來說可以隨意。
到這裡我們就已經用第一種方式寫好了,為了驗證成功與否,再寫一個main方法驗證一下:

package springTest2;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class MainTest {    public static void main(String[] args) {        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConfigTest.class);                System.out.println("#################################");        context.close();    }}

運行之後結果

根據列印順序可以看到,首先是建構函式,也就是建立了bean,緊接著執行了init,然後再context.close要銷毀bean之前又執行了destroy。

二、JSR-250註解的方式(需要匯入jsr250-api的jar包):
首先依然是建立一個擁有構造方法在內的三個方法的java類:

package springTest2;import javax.annotation.PostConstruct;import javax.annotation.PreDestroy;public class Test2 {    @PostConstruct    public void init() {        System.out.println("this is init method2");    }    public Test2() {        super();        System.out.println("建構函式2");    }    @PreDestroy    public void destroy() {        System.out.println("this is destroy method2");    }}

很顯然,這裡和上一個類不同的是,在init和destroy方法上加入了兩個註解,@PostConstruct和上邊@Bean後的initMethod相同,而@PreDestroy則是和destroyMethod做用相同。
既然這裡有了區別,已經指定了init method和destroy method,那麼後邊聲明bean的時候自然也會有不同,也就不需要再指定一遍:

package springTest2;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@ComponentScan("springTest2")public class ConfigTest {    @Bean    Test2 test2() {        return new Test2();    }}

所以,如上代碼中只需要簡單的聲明這是一個bean就可以了,類上邊的兩個註解和上一個例子中的意思相同。
再測試一下:

package springTest2;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class MainTest {    public static void main(String[] args) {        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConfigTest.class);                System.out.println("#################################");        context.close();    }}

結果如下:

三、xml配置的方式:
這種方式實際上是和第一種對應的,只不過細節上略有改變而已,首先,建立的java類完全一樣:

package springTest2;public class Test3 {    public void init() {        System.out.println("this is init method3");    }    public Test3() {        super();        System.out.println("建構函式3");    }    public void destroy() {        System.out.println("this is destroy method3");    }    public void test() {        System.out.println("testttttttt");    }}

不同的地方就在於,第一個例子中是使用註解告訴spring這個類相當於一個設定檔,而這裡則是實實在在的設定檔spring.xml:

<?xml version="1.0" encoding="UTF-8"?>  <beans xmlns="http://www.springframework.org/schema/beans"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans.xsd">  <bean id="initOrDestroyTest" class="springTest2.Test3" init-method="init" destroy-method="destroy"></bean></beans>

這個配置大概也能算是spring.xml中最簡單的一個配置了吧,除開必要的檔案頭,就只有一個bean,而且bean裡邊也只有id,calss和init以及destroy方法。
因為簡單,所以一目瞭然,id只是為了其他地方引用,class是指定這個bean對應的類,而後邊兩個屬性則和用@Bean聲明時一模一樣。
因為這裡聲明bean和指定兩個方法是用的xml配置,因此在測試的時候也就需要稍微有一點點改變:

package springTest2;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MainTest {    public static void main(String[] args) {        ClassPathXmlApplicationContext context1 = new ClassPathXmlApplicationContext("spring.xml");        System.out.println("#################################");        context1.close();    }}

區別在於這裡直接載入了設定檔,而不是java類,使用的是ClassPathxXmlApplicationContext而不是AnnotationConfigApplicationContext。
結果如下:

這裡需要說明的一點是,在實際的web應用使用時,可以在web.xml中使用類似下邊的配置來載入bean,實現init method:

<servlet-name>dispatcher</servlet-name>      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>      <init-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:spring.xml</param-value>      </init-param>      <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>      <servlet-name>dispatcher</servlet-name>      <url-pattern>/</url-pattern>    </servlet-mapping> 

然後啟動tomcat結果如下:

這裡邊沒有調用destroy method,原因是spring本身代碼就需要我們手動調用銷毀bean的方法,像前邊的幾個例子中的context.close就是。
如果不手動調用這個方法,bean就不會被銷毀,也就不會去調用destroy method,這也就是為何這裡在web.xml中配置後,啟動tomcat 只列印了建構函式和init方法中的內容。

例子都是很簡單的,而通過簡單的例子對比可能能更進一步理解相關的知識,理解了才能在實際應用中更好的進行選擇和整合。

java代碼中init method和destroy method的三種使用方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.