美文网首页3it其他零散知识点
Spring Bean 生命周期流程

Spring Bean 生命周期流程

作者: happyJared | 来源:发表于2019-10-04 09:17 被阅读0次

    总结下 Spring Bean 生命周期流程,大概是这样的:

    • Bean 容器找到配置文件中 Spring Bean 的定义。
    • Bean 容器利用 Java Reflection API 创建 Bean 的实例。
    • 如果涉及到属性值,就利用 set 方法进行设置。
    • 如果 Bean 实现了 BeanNameAware 接口,就调用 setBeanName() 方法,并传入 Bean 的名字。
    • 如果 Bean 实现了 BeanClassLoaderAware 接口,就调用 setBeanClassLoader() 方法,以及传入 ClassLoader 对象的实例。
    • 如果 Bean 实现了 BeanFactoryAware 接口,调用 setBeanClassLoader() 方法,传入 ClassLoader 对象的实例。
    • 与上面的类似,如果实现了其他 Aware 接口,就调用相应的方法。
    • 如果有和加载这个 Bean 的 Spring 容器相关的 BeanPostProcessor 对象,执行 postProcessBeforeInitialization() 方法
    • 如果 Bean 实现了 InitializingBean 接口,执行 afterPropertiesSet() 方法。
    • 如果 Bean 在配置文件中的定义包含 init-method 属性,执行指定的方法。
    • 如果有和加载这个 Bean 的 Spring 容器相关的 BeanPostProcessor 对象,执行 postProcessAfterInitialization() 方法
    • 当要销毁 Bean 的时候,如果 Bean 实现了 DisposableBean 接口,执行 destroy() 方法。
    • 当要销毁 Bean 的时候,如果 Bean 在配置文件中的定义包含 destroy-method 属性,执行指定的方法。
    Spring Bean 生命周期流程 Spring Bean 生命周期流程

    相关文章

      网友评论

        本文标题:Spring Bean 生命周期流程

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