一:生命周期执行的过程如下:

对于一个Bean对象来说,它的生命周期有实例化-->初始化-->销毁三大块组成。所以会有如下对三大块前后做定制化Bean。

而对于Bean对象另一份的Spring感知接口来说,会有如下代码和类进行支持。

  1. ApplicationContextAwareProcessor对一些感知接口处理。详细看invokeAwareInterfaces方法。
  1. class ApplicationContextAwareProcessor implements BeanPostProcessor {
  2.  
  3. private final ConfigurableApplicationContext applicationContext;
  4.  
  5. private final StringValueResolver embeddedValueResolver;
  6.  
  7. /**
  8. * Create a new ApplicationContextAwareProcessor for the given context.
  9. */
  10. public ApplicationContextAwareProcessor(ConfigurableApplicationContext applicationContext) {
  11. this.applicationContext = applicationContext;
  12. this.embeddedValueResolver = new EmbeddedValueResolver(applicationContext.getBeanFactory());
  13. }
  14. private void invokeAwareInterfaces(Object bean) {
  15. if (bean instanceof Aware) {
  16. if (bean instanceof EnvironmentAware) {
  17. ((EnvironmentAware) bean).setEnvironment(this.applicationContext.getEnvironment());
  18. }
  19. if (bean instanceof EmbeddedValueResolverAware) {
  20. ((EmbeddedValueResolverAware) bean).setEmbeddedValueResolver(this.embeddedValueResolver);
  21. }
  22. if (bean instanceof ResourceLoaderAware) {
  23. ((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext);
  24. }
  25. if (bean instanceof ApplicationEventPublisherAware) {
  26. ((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext);
  27. }
  28. if (bean instanceof MessageSourceAware) {
  29. ((MessageSourceAware) bean).setMessageSource(this.applicationContext);
  30. }
  31. if (bean instanceof ApplicationContextAware) {
  32. ((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);
  33. }
  34. }
  35. }
  36. }

Bean感知接口处理。

AbstractAutowireCapableBeanFactory.java的invokeAwareMethods(final String beanName, final Object bean)方法上处理

  1. private void invokeAwareMethods(final String beanName, final Object bean) {
  2. if (bean instanceof Aware) {
  3. if (bean instanceof BeanNameAware) {
  4. ((BeanNameAware) bean).setBeanName(beanName);
  5. }
  6. if (bean instanceof BeanClassLoaderAware) {
  7. ClassLoader bcl = getBeanClassLoader();
  8. if (bcl != null) {
  9. ((BeanClassLoaderAware) bean).setBeanClassLoader(bcl);
  10. }
  11. }
  12. if (bean instanceof BeanFactoryAware) {
  13. ((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);
  14. }
  15. }
  16. }

1) spring对bean进行实例化,默认bean是单例。
2) spring对bean进行依赖注入。
3) 如果bean实现了BeanNameAware接口,spring将bean的id传给setBeanName()方法。
4) 如果bean实现了BeanFactoryAware接口,spring将调用setBeanFactory方法,将BeanFactory实例传进来。
5) 如果bean实现了ApplicationContextAware()接口,spring将调用setApplicationContext()方法将应用上下文的引用传入。
6) 如果bean实现了BeanPostProcessor接口,spring将调用它们的postProcessBeforeInitialization接口方法。
7) 如果bean实现了InitializingBean接口,spring将调用它们的afterPropertiesSet接口方法,类似的如果bean使用了init-method属性声明了初始化方法,改方法也会被调用。
8) 如果bean实现了BeanPostProcessor接口,spring将调用它们的postProcessAfterInitialization接口方法。
9) 此时bean已经准备就绪,可以被应用程序使用了,他们将一直驻留在应用上下文中,直到该应用上下文被销毁。
10) 若bean实现了DisposableBean接口,spring将调用它的distroy()接口方法。同样的,如果bean使用了destroy-method属性声明了销毁方法,则该方法被调用。

这里一用仓颉的一幅图说明流程: 转载自 https://www.cnblogs.com/xrq730/p/6363055.html

第二幅图解释:

二:代码测试

  1.  
  1. /**
    * @see org.springframework.beans.factory.config.BeanFactoryPostProcessor
    * @see org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter
    * @see org.springframework.beans.factory.InitializingBean
    * @see org.springframework.beans.factory.DisposableBean
    * @see org.springframework.beans.factory.BeanNameAware
    * @see org.springframework.beans.factory.BeanFactoryAware
    * @see org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBean(String, RootBeanDefinition, Object[])
    **/
  1. @Slf4j
  2. @Component
  3. public class SpringBean implements BeanNameAware, BeanFactoryAware, InitializingBean, ApplicationContextAware, DisposableBean {
  4.  
  5. public SpringBean() {
  6. log.info("new SpringBean......");
  7. }
  8.  
  9. @Override
  10. public void setApplicationContext(ApplicationContext context) throws BeansException {
  11. log.info("ApplicationContextAware-setApplicationContext......");
  12. }
  13.  
  14. @Override
  15. public void afterPropertiesSet() throws Exception {
  16. log.info("InitializingBean-afterPropertiesSet......");
  17. }
  18.  
  19. @Override
  20. public void setBeanFactory(BeanFactory bf) throws BeansException {
  21. log.info("BeanFactoryAware-setBeanFactory......");
  22. }
  23.  
  24. @Override
  25. public void setBeanName(String name) {
  26. log.info("BeanNameAware-setBeanName......");
  27. }
  28.  
  29. @Override
  30. public void destroy() throws Exception {
  31. log.info("DisposableBean-destroy.....");
  32. }
  33. }
  1. @Component
  2. @Slf4j
  3. public class SpringBeanPostProcessor implements BeanPostProcessor {
  4.  
  5. @Override
  6. public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
  7. if (o instanceof SpringBean) {
  8. log.info("BeanPostProcessor-postProcessBeforeInitialization......");
  9. }
  10. return o;
  11. }
  12.  
  13. @Override
  14. public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
  15. if (o instanceof SpringBean) {
  16. log.info("BeanPostProcessor-postProcessAfterInitialization......");
  17. }
  18. return o;
  19. }
  20. }

结果展示

Spring的Bean的生命周期的更多相关文章

  1. JAVA面试题:Spring中bean的生命周期

    Spring 中bean 的生命周期短暂吗? 在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean的别名只能维持一 ...

  2. 深入理解Spring中bean的生命周期

    [Spring中bean的生命周期] bean的生命周期 1.以ApplocationContext上下文单例模式装配bean为例,深入探讨bean的生命周期: (1).生命周期图: (2).具体事例 ...

  3. Spring中Bean的生命周期及其扩展点

    原创作品,可以转载,但是请标注出处地址http://www.cnblogs.com/V1haoge/p/6106456.html Spring中Bean的管理是其最基本的功能,根据下面的图来了解Spr ...

  4. 简:Spring中Bean的生命周期及代码示例

    (重要:spring bean的生命周期. spring的bean周期,装配.看过spring 源码吗?(把容器启动过程说了一遍,xml解析,bean装载,bean缓存等)) 完整的生命周期概述(牢记 ...

  5. 面试Spring之bean的生命周期

    找工作的时候有些人会被问道Spring中Bean的生命周期,其实也就是考察一下对Spring是否熟悉,工作中很少用到其中的内容,那我们简单看一下. 在说明前可以思考一下Servlet的生命周期:实例化 ...

  6. 通过BeanPostProcessor理解Spring中Bean的生命周期

    通过BeanPostProcessor理解Spring中Bean的生命周期及AOP原理 Spring源码解析(十一)Spring扩展接口InstantiationAwareBeanPostProces ...

  7. 一分钟掌握Spring中bean的生命周期!

    Spring 中bean 的生命周期短暂吗? 在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean 的别名只能维持 ...

  8. Spring中bean的生命周期!

    Spring 中bean 的生命周期短暂吗? 在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean的别名只能维持一 ...

  9. 深究Spring中Bean的生命周期

    前言 这其实是一道面试题,是我在面试百度的时候被问到的,当时没有答出来(因为自己真的很菜),后来在网上寻找答案,看到也是一头雾水,直到看到了<Spring in action>这本书,书上 ...

  10. Spring中 bean的生命周期

    为什么要了解Spring中 bean的生命周期? 有时候我们需要自定义bean的创建过程,因此了解Spring中 bean的生命周期非常重要. 二话不说先上图: 在谈具体流程之前先看看Spring官方 ...

随机推荐

  1. 【37.50%】【codeforces 732D】Exams

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  2. 【Codeforces Round #438 C】 Qualification Rounds

    [链接]h在这里写链接 [题意] 给你n个问题,每个人都知道一些问题. 然后让你选择一些问题,使得每个人知道的问题的数量,不超过这些问题的数量的一半. [题解] 想法题. 只要有两个问题. 这两个问题 ...

  3. Real-time storage area network

    A cluster of computing systems is provided with guaranteed real-time access to data storage in a sto ...

  4. 如何只安装dependencies

    npm install --production 这样就会跳过devDependencies配置 参考链接 https://docs.npmjs.com/cli/install (如果想安装devDe ...

  5. 程序员,用NuGet管理好你的包包(转)

    每个女人都有很多包包:其实男人也有,但只有会写程序的男人才有 —— 代码世界中的大“包”小“包”.这些大包小包,有花钱买的,有从开源市场淘的,也有自己或同事亲手制作的. 包包有个特点:容易坏,更新快, ...

  6. 关于java中继承抽象类和实现接口的区别

    简单来说,继承就是“是不是”,实现就是“有没有”.(一个大神说的,我觉得很生动很形象 海子大神链接http://www.cnblogs.com/dolphin0520/p/3811437.html)

  7. Python: 文件操作与数据读取

    文件及目录操作 python中对文件.文件夹(文件操作函数)的操作需要涉及到os模块,主要用到的几个函数是, import os 返回指定目录下的所有文件和目录名: os.listdir() 重命名: ...

  8. WPF InkCanvas 毛笔效果

    原文:WPF InkCanvas 毛笔效果 1.先来看看InkCanvas的一般用法: <InkCanvas>     <InkCanvas.DefaultDrawingAttrib ...

  9. Coverage数据拓扑

    什么是Coverage?   Coverage数据模型源于ESRI公司1981年推出的第一个商业GIS软件——ArcInfo.也被称为地理相关数据模型(Georelational Data Model ...

  10. PHP中间uniqid在高并发重复问题

    在公用事业最近项目生成token检查问题.首先考虑php中间uniqid()函数生成一个随机字符串,但因为该函数的良好似基于微秒的水平.在高并发的情况下,,也能够产生相同的值. 解1:uniqid(r ...