对于prototype作用域的Bean,Spring容器仅仅负责创建,当客户创建了Bean实例之后,Bean实例完全交给客户端代码管理,容器不再跟踪其生命周期。对于singleton作用域的Bean,Spring容器知道Bean何时实例化结束、何时销毁,Spring可以管理实例化结束之后和销毁之前的行为。管理Bean的生命周期行为主要有如下两个时机。

1)        注入依赖关系之后

2)        即将销毁Bean之前

1.注入依赖关系之后

Spring提供两种行为方式在Bean全部属性设置成功后执行特定行为。第一种方式,使用ini-method属性指定某个方法应在Bean全部依赖关系设置结束后自动执行。第二种方式,就是让Bean类实现InitialzingBean接口,该接口提供一个方法,void affterPropertiesSet() throws Exception.下面的例子中包含运用这两种方法。不过要实际开发中建议使用第一种方法,因为代码污染小。

1)        定义个Bean的类。

public class Chinese implements Person , InitializingBean, BeanNameAware, ApplicationContextAware
{private Axe axe;public void setBeanName(String beanName){System.out.println("===setBeanName===");}public void setApplicationContext(ApplicationContext ctx){System.out.println("===setApplicationContext===");}public Chinese(){System.out.println("Spring实例化主调bean:Chinese实例...");}// axe的setter方法public void setAxe(Axe axe){System.out.println("Spring调用setAxe()执行依赖注入...");this.axe = axe;}public void useAxe(){System.out.println(axe.chop());}// 测试用的初始化方法public void init(){System.out.println("正在执行初始化方法 init...");}// 实现InitializingBean接口必须实现的方法public void afterPropertiesSet() throws Exception{System.out.println("正在执行初始化方法 afterPropertiesSet...");}
}

上面的init()方法名可以不叫这个名字,这个方法主要是用于bean.xml中配置在init-method=””中的方法,而实例了InitializingBean的接口后,就要去重写afterPropertiesSet()的方法。当然,这个Bean中继承了BeanNameAware、ApplicationContextAware接口。

2)        在配置bean.xml文件。

 <?xml version="1.0" encoding="GBK"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://www.springframework.org/schema/beans"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsd"><bean id="steelAxe" class="com.owen.app.service.impl.SteelAxe"/><!--  配置chinese Bean,使用init-method="init"指定该Bean所有属性设置完成后,自动执行init方法 --><bean id="chinese" class="com.owen.app.service.impl.Chinese"init-method="init"><property name="axe" ref="steelAxe"/></bean>
</beans>

3)        运行结果。

Spring实例化依赖bean:SteelAxe实例…

Sprting实例化主调bean:Chinese实例

Spring调用setAxe()执行依赖注入…

===调用setBeanName===

===setApplicationContext===

正在执行初始化方法afterPropertiesSet…

正在执行初始化方法ini…

钢斧砍柴真棒

4)        说明

如果既采用init-method属性指定初始化方法,又实现InitializingBean接口来指定初始化方法,Spring容器会执行两个初始化方法:先执行InitializingBean接口中定义的方法,然后执行init-method属性指定的方法。

2.即将销毁Bean之前

与制定初始化行为相似,Spring也提供两种方式制定Bean实例销毁之前的特定行为。第一种方式,使用destroy-method属性指定某个方法在Bean销毁之前被自动执行。第二种方法,就是让Chinese类实现DisposableBean接口,这个接口提供了一个方法 :void destroy() throwes Exception.下面用例子来说明。

1)        创建一个Bean.

public class Chinese implements Person,DisposableBean
{private Axe axe;public Chinese(){System.out.println("Spring实例化主调bean:Chinese实例...");}public void setAxe(Axe axe){System.out.println("Spring执行依赖关系注入...");this.axe = axe;}public void useAxe(){System.out.println(axe.chop());}public void close(){System.out.println("正在执行销毁之前的方法 close...");}public void destroy() throws Exception{System.out.println("正在执行销毁之前的方法 destroy...");}
}

这个类使用了DisposableBean的接口,所以必须去实现destory()的方法,而对于close()的方法,可能不叫这个名字,这个方法名主要是用在bean.xml文件中配置在destory-method=””中的。

2)        配置bean.xml.

<?xml version="1.0" encoding="GBK"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://www.springframework.org/schema/beans"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsd"><bean id="steelAxe" class="com.owen.app.service.impl.SteelAxe"/><!--  配置chinese Bean,使用destroy-method="close"指定该Bean实例被销毁之前,Spring会自动执行指定该Bean的close方法 --><bean id="chinese" class="com.owen.app.service.impl.Chinese"destroy-method="close"><property name="axe" ref="steelAxe"/></bean>
</beans>

3)        写个测试类,用registerShutdownHook()方法来关闭容器中注册的Bean.

public class BeanTest
{public static void main(String[] args){// 以CLASSPATH路径下的配置文件创建ApplicationContextAbstractApplicationContext ctx = newClassPathXmlApplicationContext("beans.xml");// 获取容器中的Bean实例Person p = ctx.getBean("chinese" , Person.class);p.useAxe();// 为Spring容器注册关闭钩子ctx.registerShutdownHook();}
}

4)        运行结果。

正在执行销毁之前的方法destory…

正在执行销毁之前的方法 close…

5)        说明。

6)        如果既采用destroy-method属性指定初始化方法,又实现DisposableBean接口来指定初始化方法,Spring容器会执行两个初始化方法:先执行DisposableBean接口中定义的方法,然后执行destroy-method属性指定的方法。

容器中Bean的生命周期相关推荐

  1. IOC容器中bean的生命周期,iocbean生命周期

    原文地址:http://www.bkjia.com/Javabc/1149957.html IOC容器中bean的生命周期,iocbean生命周期 一.Bean的生命周期 Spring IOC容器可以 ...

  2. IoC基础篇(一)--- Spring容器中Bean的生命周期

    IoC基础篇(一)--- Spring容器中Bean的生命周期 日出日落,春去秋来,花随流水,北雁南飞,世间万物皆有生死轮回.从调用XML中的Bean配置信息,到应用到具体实例中,再到销毁,Bean也 ...

  3. IOC容器中bean的生命周期

    一.Bean的生命周期 Spring IOC容器可以管理Bean的生命周期,允许在Bean生命周期的特定点执行定制的任务. Spring IOC容器对Bean的生命周期进行管理的过程如下: (1).通 ...

  4. SpringIOC容器中Bean的生命周期

    SpringIOC容器 可以管理Bean的生命周期 Spring允许在Bean生命周期的特定点,执行定制的任务. SpringIOC容器中,Bean的生命周期如下: 1.通过构造器或工厂方法创建Bea ...

  5. 面试问题:Spring中Bean 的生命周期

    Spring Bean生命周期执行流程 在传统的 Java 应用中,Bean 的生命周期很简单,使用关键字 new 实例化 Bean,当不需要该 Bean 时,由 Java 自动进行垃圾回收. Spr ...

  6. Spring 中 Bean 的生命周期

    本文作者: 伯乐在线 - yemengying 智商捉鸡?,实在没办法一下子理解Spring IoC和AOP的实现原理,看的闹心也不太懂,所以...决定拆成小的不能在小的一个个问题,一点点啃.今天先来 ...

  7. spring bean生命周期_Spring中bean的生命周期和扩展点有哪些?

    前言 今天和大家分享一下Spring中Bean的生命周期的一些知识.先来说一下什么是生命周期吧,生命周期从其语义上理解就是一个对象从产生到销毁的整个过程,之所以把这个过程称为生命周期是因为其就像一个生 ...

  8. (十)Spring中Bean的生命周期(下)

    Spring官网阅读(十)Spring中Bean的生命周期(下) 在上篇文章中,我们已经对Bean的生命周期做了简单的介绍,主要介绍了整个生命周期中的初始化阶段以及基于容器启动停止时LifeCycle ...

  9. java 中 bean 的生命周期

    java 中 bean 的生命周期 本篇中会对涉及到的知识点皆做出描述: 首先,我们先了解先虚拟机的类加载机制: 虚拟机把描述类的数据从Class 文件中加载到内存,并对数据进行校验.转换解析和初始化 ...

  10. 如果你每次面试前都要去背一篇Spring中Bean的生命周期,请看完这篇文章

    前言 当你准备去复习Spring中Bean的生命周期的时候,这个时候你开始上网找资料,很大概率会看到下面这张图: 先不论这张图上是否全面,但是就说这张图吧,你是不是背了又忘,忘了又背? 究其原因在于, ...

最新文章

  1. Android应用如何开机自启动、自启动失败原因
  2. 基于JavaWeb实现网上花店商城系统
  3. win10桌面和手机的扩展API,判断是否有实体后退键API
  4. Dubbo服务发布调用
  5. “程序”二字的五笔字根
  6. == 和 is 的区别
  7. 多线程、多平台环境中的跟踪 - 使用 log4j 和 UNIX 命令来挖掘跟踪数据
  8. java服务器代码_简单java服务器
  9. opencv 提取图像线条 霍夫线检测
  10. Intel® QAT 助力Nginx压缩处理
  11. Springboot+Vue+EasyExcel实现web页面的excel下载
  12. 诗歌《船》 ---白桦 勉励自己
  13. win7修改计算机名访问被拒绝访问,今天解答win7无法更改注册表拒绝访问的解决介绍...
  14. 摆扫式(whisk broom)和推扫式(push broom)卫星传感器
  15. tableau 连接python
  16. 柬埔寨月薪4万敲代码?真相是我差点丢了性命!
  17. 51单片机的1T和12T的区别
  18. mysql第二天无法连接_MySQL第二天早上第一次连接超时报错,解决方法com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:...
  19. 阿里LinkWAN SDK使用之mqtt数据流
  20. 《Adobe Fireworks CS6中文版经典教程》——1.3使用属性面板

热门文章

  1. 有indexPath获取到cell对象
  2. linux中screen命令的用法
  3. Windows Phone开发(4):框架和页 转:http://blog.csdn.net/tcjiaan/article/details/7263146
  4. ab753变频器参数怎么拷贝到面板_20款常用变频器密码,想成为电工老师傅你一定需要,纯干货分享!...
  5. vs2017+配置工程的编译路径(输出目录和中间目录)
  6. 游戏开发之.h、.c、.hpp及.cpp的区别
  7. IPv6 gre隧道、路由协议综合实验(华为设备)
  8. VRRP技术原理与注意点
  9. OSPF邻接关系建立过程
  10. 关于MPLS静态 LSP建立的问题——针对上题的另一种解法