Spring官方文檔閱讀筆記

1. The IoC Container

1.1. Introduction to the Spring IoC Container and Beans

  • In Spring, the objects that form the backbone of your application and that are managed by the Spring IoC container are called beans.
  • A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container.
  • Beans, and the dependencies among them, are reflected in the configuration metadata used by a container.
  • 在這裏插入圖片描述

Spring創建Bean的過程

  • AbstractApplicationContext.prepareRefresh():環境準備
  • AbstractApplicationContext.obtainFreshBeanFactory():創建beanFactory
    • GenericApplicationContext.getBeanFactory()
  • AbstractApplicationContext.prepareBeanFactory():beanFactory準備
  • AnnotationConfigEmbeddedWebApplicationContext.postProcessBeanFactory()
    • EmbeddedWebApplicationContext.postProcessBeanFactory():設置beanPostProcessor爲WebApplicationContextServletContextAwareProcessor對象;
  • AbstractApplicationContext.invokeBeanFactoryPostProcessors()
    • PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors()
  • AbstractApplicationContext.registerBeanPostProcessors() 註冊beanPostProcessor;
    • PostProcessorRegistrationDelegate.registerBeanPostProcessors()
  • AbstractApplicationContext.initMessageSource():初始化容器中的messageSource;
  • AbstractApplicationContext.initApplicationEventMulticaster():初始化事件分發器;
  • EmbeddedWebApplicationContext.onRefresh()
    • EmbeddedWebApplicationContext.createEmbeddedServletContainer()
  • AbstractApplicationContext.registerListeners():註冊事件監聽器
  • AbstractApplicationContext.finishBeanFactoryInitialization():
    • DefaultListableBeanFactory.preInstantiateSingletons();
      • AbstractBeanFactory.getBean()

      • AbstractBeanFactory.doGetBean()

      • transformedBeanName():轉化BeanName

      • getMergedLocalBeanDefinition():合併RootBeanDefinition

      • beforePrototypeCreation()/afterPrototypeCreation()解決循環依賴

      • 創建實例:

      • AbstractAutowireCapableBeanFactory.createBean();

      • AbstractAutowireCapableBeanFactory.doCreateBean();

      • AbstractAutowireCapableBeanFactory.createBeanInstance();

      • AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod();工廠方法創建對象;

      • AbstractAutowireCapableBeanFactory.autowireConstructor();推斷構造函數;

      • AbstractAutowireCapableBeanFactory.instantiateBean();無參構造函數;

      • 屬性注入populateBean()

      • 初始化:

        • invokeAwareMethods()
        • BeanPostProcessor.postProcessBeforeInstantiation()
        • invokeInitMethods()
          • 1、判斷bean是否繼承了InitializingBean,如果繼承接口,執行afterPropertiesSet()方法,
          • 2、獲得是否設置了init-method屬性,如果設置了,就執行設置的方法
        • BeanPostProcessor.postProcessAfterInstantiation() 鉤子
      • 類型轉化convertIfNecessary()

  • AbstractApplicationContext.finishRefresh():refresh完成

Spring銷燬Bean的過程

  • LiveBeansView.unregisterApplicationContext():卸載註冊的IOC對象
  • AbstractApplicationContext.destroyBeans():銷燬bean
    • DefaultSingletonBeanRegistry.destroySingletons():銷燬單粒bean;
      • DefaultSingletonBeanRegistry.destroySingleton()
        • DefaultSingletonBeanRegistry.destroyBean()
          • DisposableBean.destroy()
  • AbstractApplicationContext.closeBeanFactory():銷燬beanFactory對象
  • 在這裏插入圖片描述

Spring創建Bean的詳細過程

Mybatis執行SQL過程

  • MapperProxy.invoke()
  • MapperMethod.execute()
  • MapperMethod.executeForMany()
  • SqlSessionTemplate.selectList()
  • SqlSessionTemplate.invoke()
  • DefaultSqlSession.selectList()
  • this.executor.query()
  • this.query()先判斷緩存是否爲空,若爲空
  • this.delegate.query()
  • BaseExecutor.query()
  • BaseExecutor.queryFromDatabase()
  • BaseExecutor.doQuery()
  • RoutingStatementHandler.query()
  • PreparedStatementHandler.query()
  • PreparedStatementLogger.invoke()
  • StatementFacade.invoke()
  • PreparedStatement.execute()
  • PreparedStatement.executeInternal()
  • MySQLConnection.execSQL()
  • MysqlIO.sqlQueryDirect()
  • MysqlIO.sendCommand()
  • MysqlIO.send()
  • DefaultResultSetHandler.handleResultSets()
  • DefaultResultSetHandler.handleResultSet()
  • 在這裏插入圖片描述

@EnableAutoConfiguration註解

  • @EnableAutoConfiguration註解中import了EnableAutoConfigurationImportSelector類。
  • SpringBoot啓動過程的函數調用:
    • SpringApplication.run()
    • SpringApplication.createApplicationContext()註冊ConfigurationClassPostProcessor到Spring容器中;
      • AnnotatedBeanDefinitionReader.AnnotatedBeanDefinitionReader()構造函數;
        • AnnotationConfigUtils.registerAnnotationConfigProcessors()
          • RootBeanDefinition.RootBeanDefinition();
            • AnnotationConfigUtils.registerPostProcessor();
    • SpringApplication.refreshContext()調用
      • AbstractApplicationContext.refresh()
        • AbstractApplicationContext.invokeBeanFactoryPostProcessors()
        • AutoConfigurationImportSelector.selectImports()
        • AutoConfigurationImportSelector.getCandidateConfigurations()
        • SpringFactoriesLoader.loadFactoryNames()加載spring-boot-autoconfigure.jar/META-INF/spring.factories文件中的org.springframework.boot.autoconfigure.EnableAutoConfiguration對應的value;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章