星空网 > 软件开发 > Java

Spring bean 生命周期

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  }}

在基于

<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  }}

在基于

<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的回调,因为

例如:

使用Eclipse IDE,然后按照下面的步骤来创建一个Spring应用程序:

步骤描述
1Create a project with a name SpringExample and create a package com.manongjc under the src folder in the created project.
2Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter.
3Create Java classes HelloWorld and MainApp under the com.manongjc package.
4Create Beans configuration file Beans. under the src folder.
5The 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.manongjc;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.manongjc;import org.springframework.context.support.AbstractApplicationContext;import org.springframework.context.support.ClassPathpublic class MainApp {  public static void main(String[] args) {   AbstractApplicationContext context =              new ClassPath);   HelloWorld obj = (HelloWorld) context.getBean("helloWorld");   obj.getMessage();   context.registerShutdownHook();  }}

下面是需要的init和destroy方法配置文件beans.

<??><beans ="http://www.springframework.org/schema/beans"  ="http://www.w3.org/2001/  xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  <bean id="helloWorld"    class="com.manongjc.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 ="http://www.springframework.org/schema/beans"  ="http://www.w3.org/2001/  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>

原文地址:http://www.manongjc.com/spring/spring_bean_life_cycle.html

相关阅读:

  • Spring教程
  • Spring框架是什么
  • Spring框架的体系结构
  • Spring 环境安装配置
  • Spring 手把手入门实例
  • Spring IoC容器及原理
  • Spring bean定义
  • Spring Bean 的作用域
  • Spring bean 生命周期
  • Spring BeanPostProcessor 接口
  • Spring bean定义继承
  • Spring 依赖注入
  • Spring注入内部bean
  • Spring注入集合
  • Spring Beans自动装配
  • Spring事件处理
  • Spring基于注解配置
  • Spring 基于Java配置



原标题:Spring bean 生命周期

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

雄达国际物流:https://www.goluckyvip.com/tag/44265.html
熊达令:https://www.goluckyvip.com/tag/44266.html
熊猫生活:https://www.goluckyvip.com/tag/44267.html
熊猫速汇:https://www.goluckyvip.com/tag/44268.html
熊小康:https://www.goluckyvip.com/tag/44269.html
sellerapp:https://www.goluckyvip.com/tag/4427.html
加码韩国市场!亚马逊在韩推出免费送货服务:https://www.kjdsnews.com/a/1842204.html
沅陵县借母溪攻略 沅陵借母溪好玩吗:https://www.vstour.cn/a/409230.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流