java代碼中initmethod和destroymethod怎么使用-創(chuàng)新互聯(lián)

這篇文章給大家分享的是有關(guān)java代碼中init method和destroy method怎么使用的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

創(chuàng)新互聯(lián)公司擁有一支富有激情的企業(yè)網(wǎng)站制作團(tuán)隊(duì),在互聯(lián)網(wǎng)網(wǎng)站建設(shè)行業(yè)深耕十年,專業(yè)且經(jīng)驗(yàn)豐富。十年網(wǎng)站優(yōu)化營(yíng)銷經(jīng)驗(yàn),我們已為上千中小企業(yè)提供了成都做網(wǎng)站、成都網(wǎng)站制作解決方案,按需網(wǎng)站制作,設(shè)計(jì)滿意,售后服務(wù)無(wú)憂。所有客戶皆提供一年免費(fèi)網(wǎng)站維護(hù)!

在java的實(shí)際開(kāi)發(fā)過(guò)程中,我們可能常常需要使用到init method和destroy method,比如初始化一個(gè)對(duì)象(bean)后立即初始化(加載)一些數(shù)據(jù),在銷毀一個(gè)對(duì)象之前進(jìn)行垃圾回收等等。

周末對(duì)這兩個(gè)方法進(jìn)行了一點(diǎn)學(xué)習(xí)和整理,倒也不是專門為了這兩個(gè)方法,而是在鞏固spring相關(guān)知識(shí)的時(shí)候提到了,然后感覺(jué)自己并不是很熟悉這個(gè),便好好的了解一下。

根據(jù)特意的去了解后,發(fā)現(xiàn)實(shí)際上可以有三種方式來(lái)實(shí)現(xiàn)init method和destroy method。

要用這兩個(gè)方法,自然先要知道這兩個(gè)方法究竟是干嘛用的。而從字面意思就很容易理解,一個(gè)是加載,一個(gè)是銷毀。
下邊就正式代碼演示三種創(chuàng)建方式:

一、@Bean注解方式:

首先要?jiǎng)?chuàng)建一個(gè)至少擁有兩個(gè)方法的類,一個(gè)方法充當(dāng)init method,另一個(gè)充當(dāng)destroy method。

package springTest2;
public class Test1 {
  public void init() {
    System.out.println("this is init method1");
  }
  public Test1() {
    super();
    System.out.println("構(gòu)造函數(shù)1");
  }
  public void destroy() {
    System.out.println("this is destroy method1");
  }
}

這里很顯然只是一個(gè)普通的java類,擁有一個(gè)無(wú)參構(gòu)造和另外兩個(gè)方法。

需要注意的是,這里的init和destroy兩個(gè)方法名實(shí)際上是可以隨意取得,不叫這個(gè)也沒(méi)有問(wèn)題,只不過(guò)算是一種約定俗稱,一般都是這樣叫。

另外我們也知道,這個(gè)構(gòu)造方法也是可以不要的,因?yàn)闀?huì)隱式的自動(dòng)創(chuàng)建,但是為了更清楚的看到init和destroy是什么時(shí)候執(zhí)行,我們就顯示的寫出來(lái)。

創(chuàng)建好了這個(gè)類,我們就可以使用@Bean注解的方式指定兩個(gè)方法,以讓他們生效。

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這個(gè)類是一個(gè)配置類,相當(dāng)于我們的xml文件,@ComponentScan則是指定需要spring來(lái)掃描的包,相當(dāng)于xml中的context:component-scan屬性。

而@Bean后邊的initMethod和destroyMethod就是在聲明這是一個(gè)baen的同時(shí)指定了init和destroy方法,方法名從功能實(shí)現(xiàn)上來(lái)說(shuō)可以隨意。

到這里我們就已經(jīng)用第一種方式寫好了,為了驗(yàn)證成功與否,再寫一個(gè)main方法驗(yàn)證一下:

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();
  }
}

運(yùn)行之后結(jié)果如圖:

java代碼中init method和destroy method怎么使用 

根據(jù)打印順序可以看到,首先是構(gòu)造函數(shù),也就是創(chuàng)建了bean,緊接著執(zhí)行了init,然后再context.close要銷毀bean之前又執(zhí)行了destroy。

二、JSR-250注解的方式(需要導(dǎo)入jsr250-api的jar包):

首先依然是創(chuàng)建一個(gè)擁有構(gòu)造方法在內(nèi)的三個(gè)方法的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("構(gòu)造函數(shù)2");
  }
  @PreDestroy
  public void destroy() {
    System.out.println("this is destroy method2");
  }
}

很顯然,這里和上一個(gè)類不同的是,在init和destroy方法上加入了兩個(gè)注解,@PostConstruct和上邊@Bean后的initMethod相同,而@PreDestroy則是和destroyMethod做用相同。
既然這里有了區(qū)別,已經(jīng)指定了init method和destroy method,那么后邊聲明bean的時(shí)候自然也會(huì)有不同,也就不需要再指定一遍:

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();
  }
}

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

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();
  }
}

結(jié)果如下:

java代碼中init method和destroy method怎么使用

三、xml配置的方式:

這種方式實(shí)際上是和第一種對(duì)應(yīng)的,只不過(guò)細(xì)節(jié)上略有改變而已,首先,創(chuàng)建的java類完全一樣:

package springTest2;
public class Test3 {
  public void init() {
    System.out.println("this is init method3");
  }
  public Test3() {
    super();
    System.out.println("構(gòu)造函數(shù)3");
  }
  public void destroy() {
    System.out.println("this is destroy method3");
  }
  public void test() {
    System.out.println("testttttttt");
  }
}

不同的地方就在于,第一個(gè)例子中是使用注解告訴spring這個(gè)類相當(dāng)于一個(gè)配置文件,而這里則是實(shí)實(shí)在在的配置文件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>

這個(gè)配置大概也能算是spring.xml中最簡(jiǎn)單的一個(gè)配置了吧,除開(kāi)必要的文件頭,就只有一個(gè)bean,而且bean里邊也只有id,calss和init以及destroy方法。

因?yàn)楹?jiǎn)單,所以一目了然,id只是為了其他地方引用,class是指定這個(gè)bean對(duì)應(yīng)的類,而后邊兩個(gè)屬性則和用@Bean聲明時(shí)一模一樣。

因?yàn)檫@里聲明bean和指定兩個(gè)方法是用的xml配置,因此在測(cè)試的時(shí)候也就需要稍微有一點(diǎn)點(diǎn)改變:

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();
  }
}

區(qū)別在于這里直接加載了配置文件,而不是java類,使用的是ClassPathxXmlApplicationContext而不是AnnotationConfigApplicationContext。

結(jié)果如下:

java代碼中init method和destroy method怎么使用

這里需要說(shuō)明的一點(diǎn)是,在實(shí)際的web應(yīng)用使用時(shí),可以在web.xml中使用類似下邊的配置來(lái)加載bean,實(shí)現(xiàn)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>

然后啟動(dòng)tomcat結(jié)果如下:

java代碼中init method和destroy method怎么使用 

這里邊沒(méi)有調(diào)用destroy method,原因是spring本身代碼就需要我們手動(dòng)調(diào)用銷毀bean的方法,像前邊的幾個(gè)例子中的context.close就是。

如果不手動(dòng)調(diào)用這個(gè)方法,bean就不會(huì)被銷毀,也就不會(huì)去調(diào)用destroy method,這也就是為何這里在web.xml中配置后,啟動(dòng)tomcat 只打印了構(gòu)造函數(shù)和init方法中的內(nèi)容。

例子都是很簡(jiǎn)單的,而通過(guò)簡(jiǎn)單的例子對(duì)比可能能更進(jìn)一步理解相關(guān)的知識(shí),理解了才能在實(shí)際應(yīng)用中更好的進(jìn)行選擇和集成。

感謝各位的閱讀!關(guān)于“java代碼中init method和destroy method怎么使用”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站muchs.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

分享名稱:java代碼中initmethod和destroymethod怎么使用-創(chuàng)新互聯(lián)
本文網(wǎng)址:http://muchs.cn/article10/djgcdo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、定制開(kāi)發(fā)網(wǎng)站維護(hù)、微信公眾號(hào)、ChatGPT軟件開(kāi)發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營(yíng)