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

  • 相关阅读:
    iOS开发Quzrtz2D 十:圆形图片的绘制以及加边框圆形图片的绘制
    团队项目(周日站立会议)
    团队项目(周六站立会议)
    团队项目(spring会议)
    团队项目(第一次会议)
    结对开发项目 电梯调度发布版(已完成) 刘佳琪、兰梦
    结对测试二(求二维数组的子数组之和最大值)(兰梦、刘佳琪)
    敏捷开发(对于敏捷开发模式的理解)
    上课结对测试项目(求整数数组的子数组之和的最大值)(兰梦,刘佳琪)
    结对开发项目:多部电梯调度(一)(兰梦、刘佳琪)
  • 原文地址:https://www.cnblogs.com/PengChengLi/p/11836863.html
Copyright © 2011-2022 走看看