bean的生命周期很容易理解。当bean被实例化时,它或许会需要执行一些初始化到达一种可用的状态。相同,当bean不再需要的时候它从容器中移除,一些清理的工作需要去做。
尽管,一系列的活动都会在bean实例化和销毁时发生,但本章只讨论生命周期中两个重点的回调方法,一个是在bean实例化时,一个是在销毁时。
要定义安装和拆除一个bean,我们通常简单的声明<bean>用 init-method和destroy-method参数。init-method属性指明在bean被实例化后立即调用。相同,destroy-method属性指明bean在被从容器中移除前调用。
初始化回调
org.springframework.beans.factory.InitializingBean接口定义了一个单独的方法:
public void afterPropertiesSet() throws Exception
因此你可以简单的继承上面的接口并在afterPropertiesSet()方法中做初始化工作,如下:
public class ExampleBean implements InitializingBean {
public void afterPropertiesSet() {
// do some initialization work
}
}
在基于XML的配置元数据的情况下,你可以用init-method属性指明一个没有参数签名的方法。例如:
<bean id="exampleBean" class="examples.ExampleBean" init-method="init" />
如下是类定义:
public class ExampleBean {
public void init() {
// do some initialization work
}
}
销毁回调
org.springframework.beans.factory.DisposableBean接口定义了唯一一个方法:
public void destroy() throws Exception
因此你可以简单的继承上面的接口在destroy()方法中做一些终结工作,如下:
public class ExampleBean implements DisposableBean {
public void destroy() {
// do some destruction work
}
}
在基于XML配置元数据的情况下,你可以使用destroy-method属性指明一个无参签名的方法,例如:
<bean id="exampleBean" class="examples.ExampleBean" destroy-method="destroy" />
如下是类定义:
public class ExampleBean {
public void destroy() {
// do some destruction work
}
}
如果你使用Spring IoC容器在非web的应用环境,例如,在富客户端的桌面环境;你在JVM中注册一个关闭钩子。这样做确保完美的关闭,并且调用你的singleton类型的bean的销毁方法,释放所有的资源。
建议你不要使用InitializingBean和DisposableBean回调,因为XML配置提供了更多的灵活性在命名你的方法时。
示例让我们在Eclipse IDE中按如下步骤创建一个应用:
步骤 | 描述 |
1 | 创建一个SpringExample的项目并在src目录下创建com.tutorialspoint包。 |
2 | 在Add External JARs选项卡中添加Spring库如在Spring Hello World章节中所讲。 |
3 | 在com.tutorialspoint包下创建HelloWorld和MainApp类。 |
4 | 在src目录下创建bean的配置文件Beans.xml |
5 | 最后一步在Java类中和Bean配置文件中添加内容,并运行应用。 |
如下是HelloWorld.java源代码:
public class HelloWorld {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage (){
System.out.println ("Your Message : " + message);
}
public void init (){
System.out.println ("Bean is going through init.");
}
public void destroy (){
System.out.println ("Bean will destroy now.");
}
}
如下是MainApp.java的源代码。这里你需要注册一个关闭钩子registerShutdownHook()方法,这个方法在AbstractApplicationContext类中定义。这将确保完美的关闭和调用相关的destroy方法。
package com.tutorialspoint;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
context.registerShutdownHook();
}
}
如下是带有init-method和destroy-method方法的配置文件Beans.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-3.0.xsd">
<bean id="helloWorld" class="com.tutorialspoint.HelloWorld"
init-method="init" destroy-method="destroy">
<property name="message" value="Hello World!" />
</bean>
</beans>
一旦你已经完成了源代码和bean配置文件,运行应用,如果应用一切正常,会打印如下消息:
Bean is going through init.
Your Message : Hello World!
Bean will destroy now.
默认的初始化和销毁方法
如果你有很多bean有初始化和销毁方法有相同的名字,你不需要在每个单独的bean中声明init-method和destroy-method。这种情况框架提供了灵活的替代配置通过在<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"
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="…" class="….">
<!-- collaborators and configuration for this bean go here --> </bean>
</beans>