什么是Servlet(小服务器)容器/Spring容器

  • 它首先是一个java应用程序,跑在jvm里面
    • 同理还有tomcat jetty etc
    • 他们首先是一个普通的java程序
    • 普通的java程序被人类的指挥精巧地组织起来的成果
    • 可以对他们做一切可以对普通的java程序做的事情
  • 他们遵守java程序的规定(限定)
    • 为什么这里脱离了容器就是null
    • 我想知道这个属性是什么时候被注入的?

web应用是怎么工作的?

image.png
image.png

spring容器

核心概念

DI(dependency injection)

IOC(inverse of control)

image.png

对比二者区别

自己手工创建对象,按照依赖关系装配

声明对象的依赖关系,容器自动完成装配

@autowired/@inject

Spring源码导读

1. 容器是怎么启动的以及bean加载

  • BeanFactory(根接口)
    • ApplicationContext(这个就是spring的容器类,实现了上面的那个接口)
  • refresh模板方法
    • 读取bean定义
    • 挨个儿实例化
      • 依赖注入

AbstractApplication类的refresh方法

  1. @Override
  2. public void refresh() throws BeansException, IllegalStateException {
  3. synchronized (this.startupShutdownMonitor) {
  4. StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");
  5. // Prepare this context for refreshing.
  6. prepareRefresh();
  7. // Tell the subclass to refresh the internal bean factory.
  8. ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
  9. // Prepare the bean factory for use in this context.
  10. prepareBeanFactory(beanFactory);
  11. try {
  12. // Allows post-processing of the bean factory in context subclasses.
  13. postProcessBeanFactory(beanFactory);
  14. StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");
  15. // Invoke factory processors registered as beans in the context.
  16. invokeBeanFactoryPostProcessors(beanFactory);
  17. // Register bean processors that intercept bean creation.
  18. registerBeanPostProcessors(beanFactory);
  19. beanPostProcess.end();
  20. // Initialize message source for this context.
  21. initMessageSource();
  22. // Initialize event multicaster for this context.
  23. initApplicationEventMulticaster();
  24. // Initialize other special beans in specific context subclasses.
  25. onRefresh();
  26. // Check for listener beans and register them.
  27. registerListeners();
  28. // Instantiate all remaining (non-lazy-init) singletons.
  29. finishBeanFactoryInitialization(beanFactory);
  30. // Last step: publish corresponding event.
  31. finishRefresh();
  32. }
  33. catch (BeansException ex) {
  34. if (logger.isWarnEnabled()) {
  35. logger.warn("Exception encountered during context initialization - " +
  36. "cancelling refresh attempt: " + ex);
  37. }
  38. // Destroy already created singletons to avoid dangling resources.
  39. destroyBeans();
  40. // Reset 'active' flag.
  41. cancelRefresh(ex);
  42. // Propagate exception to caller.
  43. throw ex;
  44. }
  45. finally {
  46. // Reset common introspection caches in Spring's core, since we
  47. // might not ever need metadata for singleton beans anymore...
  48. resetCommonCaches();
  49. contextRefresh.end();
  50. }
  51. }
  52. }
  1. 创建一个applicationContext
  2. 读取所有的bean定义,并且加载
    • 工厂开张了,先招聘了一个主管叫做ConfigurationClassPostProcessor
    • 由这个主管负责找更多的人(扫描更多的bean),
      • 有的主管扫描注解
      • 有的主管读取xml
      • BeanDefinition的资源定位
        • ResourceLoader加载
    • 按照Bean的定义取创建Bean
    • 一些Hook,比如postProcessor

2. 依赖注入

构造器注入

image.png

AbstractAutowiredCapableBeanFactory

ConstructorResolver

Field注入

AutowiredAnnotationBeanPostProcessor负责注入