【译文】【学习】Spring 生命周期回调
【目标读者】
本教程是专为java编程人员设计的,用来帮助他们理解Spring 3框架以及基于它的应用。
【前置条件】
在阅读教程之前你应该有一个比较好对java语言知识的理解
【系列教程】
Introduction to spring framework
Dependency injection(ioc) in spring
Spring hello world example in eclipse
Spring java based configuaration
Dependency injection via setter method in spring
Dependency injection via constructor in spring
Spring Bean scopes with examples
Initializing collections in spring
Annotation based Configuration in spring
【Spring生命周期回调】
Spring框架提供了几个回调接口,来改变你容器中bean的行为。包括 InitializingBean 和 DisposableBean 。
Spring Bean 的生命周期应该比较容易理解。当一个bean被初始化时,它可能需要执行一些初始化的工作来使得它变成可用状态。同样地,当一个bean不再需要时并且从容器中删除时,一些清理工作是必要的。
尽管还有一些在初始化和销毁之间的活动,但最常用也最重要的就是初始化和销毁。
【Initialization callbacks】
实现org.springframework.beans.factory.InitializingBean接口允许一个bean在所有必要的属性被Spring容器注入后执行初始化工作。InitializingBean只有一个接口:
void afterPropertiesSet() throws Exception;
通常来说,可以避免 InitializingBean 接口的使用,且其也是不鼓励使用的方法,因为这已经耦合代码到Spring中。你不得不使用afterPropertiesSet()方法,且你不能更改它的名字。
有一个更好的方法是使用XML配置元数据,通过使用 bean标签属性“init-method”指定初始化方法,它可以自由更改初始化方法名字。
<bean id="countryBean" class="org.arpit.javapostsforlearning.Country" init-method="init"/> public class Country{ public void init() { // do some initialization work } }
上述代码与下面的方法效果完全一样:
<bean id="countryBean" class="org.arpit.javapostsforlearning.Country"/> public class Country implements InitializingBean { public void afterPropertiesSet() { // do some initialization work } }
基于XML的配置方法不侵入Spring。
【Destruction callbacks】
实现 org.springframework.beans.factory.DisposableBean 接口让一个bean在包含它的容器被销毁时,调用回调用函数。DisposableBean 仅有一个方法:
void destroy() throws Exception;
与InitializingBean类似,可以避免使用DisposableBean 接口,且其也是不鼓励使用。推荐使用bean 标签中的destroy-method,它可以自由更改方法名。
<bean id="countryBean" class="org.arpit.javapostsforlearning.Country" destroy-method="destroy"/> public class Country{ public void destroy() { // do some destruction work(like releasing pooled connections) } }
与下面效果一致:
<bean id="countryBean" class="org.arpit.javapostsforlearning.Country"/> public class Country implements DisposableBean{ public void destroy() { // do some destruction work(like releasing pooled connections) } }
但其不侵入Spring。
【默认的初始化和销毁方法】
在bean标签的父标签<beans> 中指定default-init-method 和 default-destroy-method 属性,如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" default-init-method="init" default-destroy-method="destroy" > <bean id="country" class="org.arpit.javapostsforlearning.Country"> <property name="countryName" value="India"/> </bean> </beans>
注: 在main中需要 有 appContext.registerShutdownHook(); 这一行,否则销毁回调不会执行。
这一条语句的意思是向JVM注册一个回调函数,在JVM停止后调用ApplicationContext的shutdownHook,这个函数里应该是会执行所有bean的销毁回调函数。
即:
Runtime.getRuntime().addShutdownHook(this.shutdownHook);

浙公网安备 33010602011771号