可以使用bean的init-method和destroy-method屬性來初始化和銷毀bean。
定義一個Hero類:
package com.moonlit.myspring; public class Hero { public void born() { System.out.println("the hero is born."); } public void defaultBorn() { System.out.println("the hero is born by default."); } public void doAction() { System.out.println("the hero save the world."); } public void dead() { System.out.println("the hero is dead."); } public void defaultDead() { System.out.println("the hero was dead by default."); } }
配置其bean:
<bean id="hero" class="com.moonlit.myspring.Hero" init-method="born" destroy-method="dead" />
還可以使用beans的default-init-method和default-destroy-method屬性來設置所有bean的默認的初始化和銷毀方法。(這種情況下如果bean有對應的方法則會執行對應的初始化和銷毀方法)。
定義一個Demon類:
package com.moonlit.myspring; public class Demon { public void defaultBorn() { System.out.println("the demon was born."); } public void doAction() { System.out.println("the demon do destroys"); } public void defaultDead() { System.out.println("the demon is dead."); } }
配置其Bean:
<bean id="demon" class="com.moonlit.myspring.Demon" />
我們配置beans的default-init-method和default-destroy-method屬性如下:
<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-3.0.xsd" default-init-method="defaultBorn" default-destroy-method="defaultDead" >
樣例程序PracticeHero用於測試效果:
package com.moonlit.practice; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.moonlit.myspring.Demon; import com.moonlit.myspring.Hero; public class PracticeHero { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "spring-idol.xml"); Hero hero = (Hero) context.getBean("hero"); hero.doAction(); Demon demon = (Demon) context.getBean("demon"); demon.doAction(); } }
輸出結果如下:
the hero is born. the demon was born. the hero save the world. the demon do destroys
不能看到銷毀的效果,可能是因為程序結束的時候對象還沒有執行銷毀的方法。(暫時還不知道怎么檢測destroy-method),但是可以看到init-method的方法。因為Hero的bean定義了init-method和destroy-method,所以程序會先找born()和dead()兩個方法執行;但是Demon的bean沒有定義init-method方法和destroy-method方法,所以程序會執行beans里面定義的default-init-method和default-destroy-method方法執行,所以輸出的效果是這樣的。
理解:使用bean元素的init-method和destroy-method元素可以定義一個bean在初始化和銷毀時使用的方法;也可以通過beans的default-init-method和default-destroy-method元素來指定所有bean的默認初始化和銷毀的方法。
還有一種方法是通過實現InitializingBean和DisposableBean接口來實現Spring的生命周期方法。InitializingBean需要實現afterPropertiesSet方法,DisposableBean需要實現destroy方法。
package com.moonlit.myspring; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Spiderman implements InitializingBean, DisposableBean { public void destroy() throws Exception { System.out.println("the spider man is dead...He will be back."); } public void doAction() { System.out.println("spider man saved Gotham City"); } public void afterPropertiesSet() throws Exception { System.out.println("The Dark Knight Rises"); } public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "spring-idol.xml"); Spiderman spiderman = (Spiderman) context.getBean("spiderman"); spiderman.doAction(); } }
輸出效果如下:
the hero is born. the demon was born. The Dark Knight Rises spider man saved Gotham City
我想這可能是因為在讀取xml文件的時候默認里面有構造方法或者沒有構造方法的bean都一起初始化了的原因吧(而我我之前定義了Hero和Demon)。
