zoukankan      html  css  js  c++  java
  • spring生命周期


    [MyBeanFactoryPostProcessor] constructor
    ...
    MyBeanFactoryPostProcessor] postProcessBeanFactory
    ...
    [MyBeanPostProcessor] constructor
    ...
    [MyInstantiationAwareBeanPostProcessor] constructor
    ...
    16:18:06.149 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'myBean'
    **MyBean** construct.
    16:18:06.154 [main] DEBUG org.springframework.context.annotation.CommonAnnotationBeanPostProcessor - Found init method on class [com.example.LifeCycle.MyBean]: public void com.example.LifeCycle.MyBean.springPostConstruct()
    16:18:06.154 [main] DEBUG org.springframework.context.annotation.CommonAnnotationBeanPostProcessor - Found destroy method on class [com.example.LifeCycle.MyBean]: public void com.example.LifeCycle.MyBean.springPreDestroy()
    16:18:06.154 [main] DEBUG org.springframework.context.annotation.CommonAnnotationBeanPostProcessor - Registered init method on class [com.example.LifeCycle.MyBean]: org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement@21a44a0a
    16:18:06.154 [main] DEBUG org.springframework.context.annotation.CommonAnnotationBeanPostProcessor - Registered destroy method on class [com.example.LifeCycle.MyBean]: org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement@5caf49c4
    16:18:06.176 [main] DEBUG org.springframework.beans.factory.annotation.InjectionMetadata - Registered injected element on class [com.example.LifeCycle.MyBean]: AutowiredMethodElement for public void com.example.LifeCycle.MyBean.setDescription(java.lang.String)
    16:18:06.176 [main] DEBUG org.springframework.beans.factory.annotation.InjectionMetadata - Registered injected element on class [com.example.LifeCycle.MyBean]: AutowiredMethodElement for public void com.example.LifeCycle.MyBean.setName(java.lang.String)
    ...
    [MyInstantiationAwareBeanPostProcessor] postProcessAfterInstantiation
    [MyInstantiationAwareBeanPostProcessor] postProcessPropertyValues
    ...
    [MyInstantiationAwareBeanPostProcessor] postProcessAfterInstantiation
    [MyInstantiationAwareBeanPostProcessor] postProcessPropertyValues
    16:18:06.177 [main] DEBUG org.springframework.beans.factory.annotation.InjectionMetadata - Processing injected element of bean 'myBean': AutowiredMethodElement for public void com.example.LifeCycle.MyBean.setDescription(java.lang.String)
    16:18:06.177 [main] DEBUG org.springframework.beans.factory.annotation.InjectionMetadata - Processing injected element of bean 'myBean': AutowiredMethodElement for public void com.example.LifeCycle.MyBean.setName(java.lang.String)
    *MyBean** setName
    **MyBean** setDescription
    **MyBean** BeanNameAware.getBeanName: myBean
    **MyBean** BeanClassLoaderAware.setBeanClassLoader: class sun.misc.Launcher$AppClassLoader
    **MyBean** BeanFactoryAware.setBeanFactory: class org.springframework.beans.factory.support.DefaultListableBeanFactory
    **MyBean** ApplicationContextAware.setApplicationContext
    [MyBeanPostProcessor] postProcessBeforeInitialization: class com.example.LifeCycle.MyBean: myBean
    ...
    **MyBean** init-method
    ...
    MyBean{name='Gigi', description='description'}
    ...
    **MyBean** @PreDestroy
    **MyBean** DisposableBean.destroy
    **MyBean** destory-method
    ————————————————

    一些建议
    不建议使用InitializingBean, DisposableBean,因为这个会增加代码与Spring框架的耦合性。
    @PostConstruct,@PreDestroy是JavaX的标准,而非Java,Spring定义的注解,使用时应该注意。
    将Bean从Spring IoC中移除之前需要释放持有的资源,建议在destroy-method中写释放资源的代码。

    应用
    了解Bean的生命周期有利于我们根据业务需要对Bean进行相关的拓展工作。
    举个例子,在Bean初始化前希望用户名和密码。倘若将这些信息硬编码到工厂代码中显然是不安全的,一般地,公司会有一个统一的密码存储服务,通过调用开放的API获取用户名和密码。此时,我们可以在Bean的init-method中加入这样的逻辑:调用公司的密码存储服务API获取用户名和密码,加载至Bean的相关字段中。
    再举个例子,在Bean被销毁之前,释放Bean与数据库的连接,那么我们可以把释放数据库连接这段逻辑放到destroy-method中。
    ————————————————


    原文链接:https://blog.csdn.net/programmer_at/article/details/82533396

  • 相关阅读:
    npm install 的时候报错 (Unexpected end of JSON input while parsing near)
    vue 父子组件以及非父子组件如何通信
    webstorm 破解版下载及破解教程------永久使用
    判断数据类型
    洗牌算法
    原生js实现图片懒加载
    React(^16.8) 新增特性Hook
    Blob、ArrayBuffer、File、FileReader和FormData的区别
    React中的Dom操作
    微信小程序填坑总结
  • 原文地址:https://www.cnblogs.com/PengChengLi/p/11836863.html
Copyright © 2011-2022 走看看