美文网首页
spring bean 生命周期

spring bean 生命周期

作者: 七宝qb | 来源:发表于2017-04-06 10:04 被阅读38次
springIOC 容器中bean 的生命周期方法
  • springioc容器可以管理bean的生命周期,spring允许在bean生命周期的特定点执行定制任务
  • springIOC 容器对bean的生命周期进行管理的过程:
    1. 通过构造器 或工厂

配置文件中 可在 <bean> 中设置 init-method destory-method 指定bean在初始化 和 销毁时的方法。

applicationContext 的子接口 ConfigurableApplicationContext 才有 bean 的close方法。

配置初始化,销毁0.png bean初始化、销毁方法测试代码.png

创建bean后置处理器

对spring bean 实例逐一扫描处理 用于检查bean实例,或者对bean属性进行更改

需实现 BeanPostProcessor 接口 。

<!--实现BeanPostProcessor 接口,并具体提供两个方法的实现
    postProcessBeforeInitialization(object o,Stirng s)   init-method  之前被调用
    postProcessAfterInitialization(object o,Stirng s)     init-method 之后被调用
    o : bean
    s :beanName
    返回值 : 是返回给用户的bean 可以在此方法钟修改bean-->
    <!--配置bean的后置处理器 不用配置id 自动识别为一个BeanPostProcessor -->
    <bean class="com.springTest.MybeanPostProcessor"></bean>

相关文章

网友评论

      本文标题:spring bean 生命周期

      本文链接:https://www.haomeiwen.com/subject/dzpfattx.html