关于spring:Spring认证Spring-Bean-生命周期教程

8次阅读

共计 3340 个字符,预计需要花费 9 分钟才能阅读完成。

Spring bean 的生命周期很容易了解。实例化 bean 时,可能须要执行一些初始化以使其进入可用状态。相似地,当不再须要 bean 并从容器中移除 bean 时,可能须要进行一些清理。

只管有在 bean 实例化和销毁之间产生的幕后活动的列表,本章将只探讨两个重要的 bean 生命周期回调办法,它们在 bean 初始化和销毁​​时须要。

要为 bean 定义设置和装配,咱们只需应用 initmethod 和 / 或 destroy-method 参数申明 <bean>。init-method 属性指定了在实例化后立刻在 bean 上调用的办法。相似地,destroymethod 指定了在从容器中删除 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 注册敞开挂钩。这样做可确保失常敞开并在单例 bean 上调用相干的 destroy 办法,以便开释所有资源。

建议您不要应用 InitializingBean 或 DisposableBean 回调,因为 XML 配置在命名办法方面提供了很大的灵活性。

示例
让咱们有一个工作的 Eclipse IDE 并采取以下步骤来创立一个 Spring 应用程序 –

这是 HelloWorld.java 文件的内容 -

package com.tutorialspoint;

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 类上申明的敞开钩子 registerShutdownHook()办法。这将确保失常敞开并调用相干的销毁办法。

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 和 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.or…d”>

<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 属性来配置这种状况的灵活性,如下所示 –

<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.or…d”
default-init-method = “init”
default-destroy-method = “destroy”>

<bean id = “…” class = “…”>

  <!-- collaborators and configuration for this bean go here -->

</bean>

</beans>

正文完
 0