菜瓜:水稻,上次说Bean的LifeCycle,还没讲完

水稻:啥?说人话?

菜瓜:spring,bean,生命周期

水稻:哦哦,下次直接说人话。说正事,先从BeanFactory、ApplicationContext和FactoryBean开始说起吧

  • 我们知道(你不一定知道)BeanFactory是Spring访问Bean容器的根接口,源码的注释: “The root interface for accessing a Spring bean container.”
  • 而ApplicationContext继承自BeanFactory,也就是说它具有BeanFactory的属性和方法,并进一步完善(继承其他的接口)
  • FactoryBean跟前两个关系就不怎么大了,它是spring提供给用户创建bean的口子,有一些bean创建过程复杂,或者依赖第三方包等(Mybatis-Spring中),可交给用户自己创建

菜瓜:嗯。。陷入沉思。。(欲言又止)

水稻:讲理论不给代码就是耍流氓

  • package com.vip.qc.postprocessor;
    
    import org.springframework.beans.factory.FactoryBean;
    import org.springframework.stereotype.Component; /**
    * @author QuCheng on 2020/6/15.
    */
    @Component("fb")
    public class FactoryBeanT implements FactoryBean<FactoryBeanT.CustomBean> { public FactoryBeanT() {
    System.out.println("实现FactoryBean接口的类自身被放在IOC一级缓存的容器里面,getObject的对象是在另一个缓存对象中");
    } @Override
    public CustomBean getObject() {
    return new CustomBean();
    } @Override
    public Class<?> getObjectType() {
    return CustomBean.class;
    } static class CustomBean {
    public CustomBean() {
    System.out.println("自定义bean");
    }
    }
    } 测试方法
    @Test
    public void testTransa() {
    BeanFactory context = new AnnotationConfigApplicationContext(ComponentScanD.class);
    System.out.println("factoryBean : " + context.getBean("fb"));
    System.out.println("&factoryBean : " + context.getBean("&fb"));
    } 测试结果

    实现FactoryBean接口的类自身被放在IOC一级缓存的容器里面,getObject的对象是在另一个缓存对象中
      自定义bean
      factoryBean : com.vip.qc.postprocessor.FactoryBeanT$CustomBean@214b199c
      &factoryBean : com.vip.qc.postprocessor.FactoryBeanT@20d3d15a

菜瓜:懂了,BeanFactory是Spring的核心--容器,ApplicationContext则是包裹容器的上下文,丰富容器的功能(资源加载,事件驱动等)。FactoryBean也是Spring扩展性的提现

水稻:WC,你这个总结提到了精髓。就是扩展性:如果BeanFactory是核心思想,那么其他的上下文,后置处理器,还是Aware接口等等,都是为了实现扩展

菜瓜:铺垫说完了,开始生命周期呗

水稻:这次咱们反过来先看源码,再看实验,再总结

  • BeanFactory源码注释 - 定义了实现的生命周期
  •  * @author Rod Johnson
    * @author Juergen Hoeller
    * @author Chris Beams
    * @since 13 April 2001
    * @see BeanNameAware#setBeanName
    * @see BeanClassLoaderAware#setBeanClassLoader
    * @see BeanFactoryAware#setBeanFactory
    * @see org.springframework.context.ResourceLoaderAware#setResourceLoader
    * @see org.springframework.context.ApplicationEventPublisherAware#setApplicationEventPublisher
    * @see org.springframework.context.MessageSourceAware#setMessageSource
    * @see org.springframework.context.ApplicationContextAware#setApplicationContext
    * @see org.springframework.web.context.ServletContextAware#setServletContext
    * @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessBeforeInitialization
    * @see InitializingBean#afterPropertiesSet
    * @see org.springframework.beans.factory.support.RootBeanDefinition#getInitMethodName
    * @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessAfterInitialization
    * @see DisposableBean#destroy
    * @see org.springframework.beans.factory.support.RootBeanDefinition#getDestroyMethodName
    */
    public interface BeanFactory {
  • BeanFactory源码实现类
  • public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory implements AutowireCapableBeanFactory
    
    protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) {
    if (System.getSecurityManager() != null) {
    AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
    invokeAwareMethods(beanName, bean);
    return null;
    }, getAccessControlContext());
    }
    else {
    // BeanNameAware BeanFactoryAware ...
    invokeAwareMethods(beanName, bean);
    } Object wrappedBean = bean;
    if (mbd == null || !mbd.isSynthetic()) {
    // BeanPostProcessor Before @PostConstruct
    wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
    } try {
    // initMethod InitializingBean接口
    invokeInitMethods(beanName, wrappedBean, mbd);
    }
    catch (Throwable ex) {
    throw new BeanCreationException(
    (mbd != null ? mbd.getResourceDescription() : null),
    beanName, "Invocation of init method failed", ex);
    }
    if (mbd == null || !mbd.isSynthetic()) {
    // BeanPostProcessor after
    wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
    } return wrappedBean;
    }
  • 实验代码
  • package com.vip.qc.postprocessor;
    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.BeanDefinition;
    import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
    import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
    import org.springframework.stereotype.Component; /**
    * @author QuCheng on 2020/6/14.
    */
    @Component
    public class BeanFactoryPostProcessorT implements BeanFactoryPostProcessor { public static final String BEAN_NAME = "initializingBeanT"; @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    BeanDefinition initializingBeanT = beanFactory.getBeanDefinition(BEAN_NAME);
    System.out.println("BeanFactoryPostProcessor bean " + initializingBeanT.getBeanClassName());
    }
    } package com.vip.qc.postprocessor; import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.BeanPostProcessor;
    import org.springframework.stereotype.Component; /**
    * @author QuCheng on 2020/6/14.
    */
    @Component
    public class BeanPostProcessorT implements BeanPostProcessor { public static final String BEAN_NAMET = "initializingBeanT"; @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    if (BEAN_NAMET.equals(beanName)) {
    InitializingBeanT processorT = ((InitializingBeanT) bean);
    System.out.println("BeanPostProcessor BeforeInitialization " + processorT);
    }
    return bean;
    } @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    if (BEAN_NAMET.equals(beanName)){
    InitializingBeanT processorT = ((InitializingBeanT) bean);
    System.out.println("BeanPostProcessor AfterInitialization " + processorT);
    }
    return bean;
    } } package com.vip.qc.postprocessor; import org.springframework.beans.factory.BeanNameAware;
    import org.springframework.beans.factory.DisposableBean;
    import org.springframework.beans.factory.InitializingBean;
    import org.springframework.stereotype.Component; import javax.annotation.PostConstruct;
    import javax.annotation.PreDestroy; /**
    * @author QuCheng on 2020/6/14.
    */
    @Component
    public class InitializingBeanT implements BeanNameAware, InitializingBean, DisposableBean { public InitializingBeanT() {
    System.out.println("init无参构造 execute");
    } @PostConstruct
    public void postConstruct() {
    System.out.println("@PostConstruct execute");
    } @PreDestroy
    public void preDestroy() {
    System.out.println("@PreDestroy execute");
    } @Override
    public void afterPropertiesSet() {
    System.out.println("InitializingBean afterPropertiesSet --> " + this.toString());
    } @Override
    public void setBeanName(String name) {
    System.out.println("BeanNameAware : " + name);
    } @Override
    public void destroy() {
    System.out.println("destroy");
    }
    } 测试代码
    @Test
    public void testLifeCycle() {
    AbstractApplicationContext applicationContext = new AnnotationConfigApplicationContext(ComponentScanD.class);
    applicationContext.close();// 这里不关闭容器的话,注销bean的方法会看不到打印
    } 测试结果

    BeanFactoryPostProcessor bean com.vip.qc.postprocessor.InitializingBeanT
      init无参构造 execute
      BeanNameAware : initializingBeanT
      BeanPostProcessor BeforeInitialization com.vip.qc.postprocessor.InitializingBeanT@15bb6bea
      @PostConstruct execute
      InitializingBean afterPropertiesSet --> com.vip.qc.postprocessor.InitializingBeanT@15bb6bea
      BeanPostProcessor AfterInitialization com.vip.qc.postprocessor.InitializingBeanT@15bb6bea
      @PreDestroy execute
      destroy

菜瓜:实现什么的不重要,接口才是爸爸呀,BeanFactory定义好了生命周期,下面的实现也只是实现罢了

水稻:哈哈,你说的对,一流的公司卖标准

菜瓜:这里怎么没看到循环依赖的处理啊

水稻:是的。这里的源码我只截取了bean初始化完成之后的接口调用。循环依赖的处理在它前面。来来来,继续刚

菜瓜:刚不了刚不了,你一下子搞这么多玩意给我看,我哪看得完

水稻:您歇着,下次您什么时候想了解我再给您说

总结

  • BeanFactory已经定义了整个的生命周期,子类只是负责实现,demo演示也只是为了证实。我们更应该关注更上层的东西
  • ApplicationContext是对容器更精细化的包装,提供了更完善的功能
  • FactoryBean是Spring扩展性的提现,可供用户自己定义创建bean。扩展性提炼的很好

【Spring】Bean的LifeCycle(生命周期)的更多相关文章

  1. spring bean 容器的生命周期是什么样的?

    spring bean 容器的生命周期流程如下: 1.Spring 容器根据配置中的 bean 定义中实例化 bean. 2.Spring 使用依赖注入填充所有属性,如 bean 中所定义的配置. 3 ...

  2. spring Bean的完整生命周期

    spring 容器中的bean的完整生命周期一共分为十一步完成. 1.bean对象的实例化 2.封装属性,也就是设置properties中的属性值 3.如果bean实现了BeanNameAware,则 ...

  3. 一张图搞懂Spring bean的完整生命周期

    一张图搞懂Spring bean的生命周期,从Spring容器启动到容器销毁bean的全过程,包括下面一系列的流程,了解这些流程对我们想在其中任何一个环节怎么操作bean的生成及修饰是非常有帮助的. ...

  4. Spring Bean各阶段生命周期的介绍

    一.xml方式配置bean 二.Aware接口 2.1 BeanNameAware 2.2 BeanFactoryAware 2.3 ApplicationContextAware 2.4 Aware ...

  5. [spring] -- bean作用域跟生命周期篇

    作用域 singleton : 唯一 bean 实例,Spring 中的 bean 默认都是单例的. prototype : 每次请求都会创建一个新的 bean 实例. request : 每一次HT ...

  6. Spring中与bean有关的生命周期

    前言 记得以前的时候,每次提起Spring中的bean相关的生命周期时,内心都无比的恐惧,因为好像有很多,自己又理不清楚,然后看网上的帖子,好像都是那么一套,什么beanFactory啊,aware接 ...

  7. Spring 了解Bean的一生(生命周期)

    转载 https://blog.csdn.net/w_linux/article/details/80086950 该篇博客就来了解IoC容器下Bean的一生吧,也可以理解为bean的生命周期. ## ...

  8. Spring注解开发系列Ⅲ --- 生命周期

    Bean的生命周期 Spring Bean 的生命周期在整个 Spring 中占有很重要的位置,掌握这些可以加深对 Spring 的理解. 首先看下生命周期图: 再谈生命周期之前有一点需要先明确: S ...

  9. Bean 注解(Annotation)配置(2)- Bean作用域与生命周期回调方法配置

    Spring 系列教程 Spring 框架介绍 Spring 框架模块 Spring开发环境搭建(Eclipse) 创建一个简单的Spring应用 Spring 控制反转容器(Inversion of ...

随机推荐

  1. SpringCloud(三)- OpenFeign简介及@FeignClient等注解的使用

    唯能极于情,故能极于剑 有问题或错误请及时联系小编或关注小编公众号 "CodeCow",小编一定及时回复和改正,期待和大家一起学习交流 此文由四部分组成(OpenFeign简介.@ ...

  2. [C#] [VS] Snippets快捷代码块之 Region

    代码长了,阅读起来不方便, 于是,C#中我们经常会用 region来折叠代码块. 在VS中,输入 #region , 点Tab,会自动生成如下: #region MyRegion #endregion ...

  3. 应小姐姐要求,整理常用Git操作命令,她都学会了,你确定不收藏

    前言 因为个人原因,转化了部门之后已经很久没有接触过开发层级的东西了,好多东西基本都忘记了,但是新的部门有时候会用到相应的研发部的代码和文档手册,所以耳边就充斥这一句话 这个为什么下载不了?这个为什么 ...

  4. C#线程 并行线程

    第五部分 并行线程   在本节中,我们将介绍Framework 4.0新增的利用多核处理器的多线程API: 并行LINQ或PLINQ Parallel 类 任务并行性构造 并发集合 自旋锁和自旋等待 ...

  5. js异步原理

    javascript引擎 javascript引擎执行时单线程的,如果有一个任务被阻塞,整个浏览器就会处于假死状态 那么遇到这种情况,又在单线程的情况下,不能像java之类的语言创建多几个线程来处理, ...

  6. Rocket - diplomacy - MixedNode

    https://mp.weixin.qq.com/s/zgeAI2n-2cHJi7-Ra5rYZA   介绍MixedNode的实现.     1. 类定义   ​​ ​​   2. inner/ou ...

  7. js实现浏览器打印功能

    最近接触到一个新需求,实现打印机打印小票的功能.打的一桌子小票(惭愧),不过也基本满足了业务上的需求,现在分享一下如何实现(好记性不如烂笔头) 先上代码 // 布局代码 <div id=&quo ...

  8. QTI EAS学习之find_energy_efficient_cpu

    Energy Awareness Scheduler是由ARM和Linaro开发的新的linux kernel调度器. 原先CFS调度器是基于policy进行调度,并有不同的吞吐量.例如,有一个新的t ...

  9. Java实现 蓝桥杯 算法训练 最大的算式

    算法训练 最大的算式 时间限制:1.0s 内存限制:256.0MB 问题描述 题目很简单,给出N个数字,不改变它们的相对位置,在中间加入K个乘号和N-K-1个加号,(括号随便加)使最终结果尽量大.因为 ...

  10. Java实现 LeetCode 48 旋转图像

    48. 旋转图像 给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转图像. 示 ...