在spring容器初始化bean和销毁bean的以前的操作有很多种,目前我知道的有:在xml中定义的时候用init-method和destory-method,
还可以使用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" >
可以使用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" />
测试
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的默认初始化和销毁的方法。
还有一种就是定义bean的时候实现DisposableBean和InitializingBean 这两个接口,
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
打开InitializingBean 的源码:
public interface InitializingBean {
/**
* Invoked by a BeanFactory after it has set all bean properties supplied
* (and satisfied BeanFactoryAware and applicationContextAware).
* <p>This method allows the bean instance to perform initialization only
* possible when all bean properties have been set and to throw an
* exception in the event of misconfiguration.
* @throws Exception in the event of misconfiguration (such
* as failure to set an essential property) or if initialization fails.
*/
void afterPropertiesSet() throws Exception;
}
根据注解很清楚的可以看出,afterPropertiesSet()表示在资源加载完以后,初始化bean之前执行的方法,我猜想spring底层应该会在初始化bean的时候,应该会使用(bean instanceof InitializingBean)判断是不是实现了这个接口,其实在很多框架中都是这么干的,但是因为没研究过spring源码,暂且还不知道底层原理。这样我们就可以在初始化的时候,做一些自己想要做的事了。
同理,DisposableBean就是在一个bean被销毁的时候,spring容器会帮你自动执行这个方法,估计底层原理也是差不多的,对于一些使用完之后需要释放资源的bean,我们都会实现这个接口,或者是配置destory-method方法。源码也基本是相似的,只是把afterPropertiesSet改为destroy。