Spring学习笔记--初始化和销毁Bean

时间:2024-01-08 22:17:02

可以使用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)。