

Spring bean的生命周期是很容易理解。当一个bean实例化时,它可能需要执行一些初始化把它转换成可用状态。类似地,当bean不再需要,并且从容器中取出,一些清理的工作可能也需要做。
不过,还有把bean背后的实例化和销毁时间之间的场景发生的活动,但是本章将只讨论其中两个是需要在bean的初始化和销毁的时候,重要bean的生命周期回调方法。
要定义安装和拆卸一个bean,我们只是声明了初始化方法和/或销毁,方法的参数<bean>。在init-method属性指定一个方法,是被调用bean后立即实例化。同样,销毁方法规定了被调用当bean被从容器中取出之前的方法。
初始化回调:
org.springframework.beans.factory.InitializingBean 接口指定一个单一的方法:
void afterPropertiesSet() throws Exception;
因此,可以简单地实现上述接口和初始化工作可以在里面afterPropertiesSet() 方法,如下所示:
public class ExampleBean implements InitializingBean {
public void afterPropertiesSet() {
// do some initialization work
}
}
在基于XML的配置元数据的情况下,可以使用init-method 属性来指定具有void无参数签名的方法的名称。例如:
<bean id="exampleBean" class="examples.ExampleBean" init-method="init"/>
下面是类的定义:
public class ExampleBean {
public void init() {
// do some initialization work
}
}
销毁回调
org.springframework.beans.factory.DisposableBean接口指定一个单一的方法:
void destroy() throws Exception;
因此,你可以简单地实现上述接口和定稿工作可以做里面的destroy() 方法,如下所示:
public class ExampleBean implements DisposableBean {
public void destroy() {
// do some destruction work
}
}
在基于XML的配置元数据的情况下,您可以使用destroy-method属性来指定具有void无参数签名的方法的名称。例如:
<bean id="exampleBean" class="examples.ExampleBean" destroy-method="destroy"/>
下面是类的定义:
public class ExampleBean {
public void destroy() {
// do some destruction work
}
}
如果您在非web应用环境中使用Spring的IoC容器,例如在桌面富客户端环境; 注册关闭钩子在JVM中。这样做可以确保正常关机,并让所有的资源都被释放调用singleton bean上的相关destroy方法。
建议不要使用的InitializingBean或者DisposableBean的回调,因为XML配置提供极大的灵活性在命名你的方法方面。
例如:
使用Eclipse IDE,然后按照下面的步骤来创建一个Spring应用程序:
步骤 | 描述 |
---|
1 | Create a project with a name SpringExample and create a package com.yiibai under the src folder in the created project. |
2 | Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter. |
3 | Create Java classes HelloWorld and MainApp under the com.yiibai package. |
4 | Create Beans configuration file Beans.xml under the src folder. |
5 | The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below. |
这里是HelloWorld.java的文件的内容:
package com.yiibai;
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文件的内容。在这里,您需要注册,是在AbstractApplicationContext 类中声明一个关机hookregisterShutdownHook() 方法。这将确保正常关机,并调用相关的destroy方法。
package com.yiibai;
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和destroy方法配置文件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.yiibai.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上。相反框架提供了灵活使用<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="init" default-destroy-method="destroy">
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
</beans>
广告 Spring技术内幕:深入解析Spring架构与设计原理
作者:计文柯
京东
