Spring经过大神们的构思、编码,日积月累而来,所以,对其代码的理解也不是一朝一夕就能快速完成的。源码学习是枯燥的,需要坚持!坚持!坚持!当然也需要技巧,第一遍学习的时候,不用关注全部细节,不重要的代码可以先忽略掉,达到理解大体的架子及流程,避免第一次就陷入某个坑里出不来。第二遍针对某个流程更深入的、有针对性的去分析学习,当然遇到某个实在过不去的坎可以标记,后面再思考,毕竟是别人设计的,有些不是那么容易理解,可以使用google,次数多了,总会有收获!

概括的描述一下Spring背后的操作,解析applicationgContext.xml,将xml中定义的bean(如loginService和loginResource)解析成Spring内部的BeanDefinition,并以beanName(如loginService)为key,BeanDefinition(如loginService相应的BeanDefinition)为value存储到DefaultListableBeanFactory中的beanDefinitionMap(其实就是一个ConcurrentHashMap)中,同时将beanName存入beanDefinitionNames(List类型)中,然后遍历beanDefinitionNames中的beanName,进行bean的实例化并填充属性,在实例化的过程中,如果有依赖没有被实例化将先实例化其依赖,然后实例化本身,实例化完成后将实例存入单例bean的缓存中,当调用getBean方法时,到单例bean的缓存中查找,如果找到并经过转换后返回这个实例(如LoginResource的实例),之后就可以直接使用了。

上边提到在Spring容器启动的过程中,会将Bean解析成Spring内部的BeanDefinition结构,BeanDefinition的内部结构。直接看BeanDefinition源码

  1. public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement {
  2. /**
  3. * Scope identifier for the standard singleton scope: "singleton".
  4. * <p>Note that extended bean factories might support further scopes.
  5. */
  6. String SCOPE_SINGLETON = ConfigurableBeanFactory.SCOPE_SINGLETON;
  7. /**
  8. * Scope identifier for the standard prototype scope: "prototype".
  9. * <p>Note that extended bean factories might support further scopes.
  10. */
  11. String SCOPE_PROTOTYPE = ConfigurableBeanFactory.SCOPE_PROTOTYPE;
  12. /**
  13. * Role hint indicating that a {@code BeanDefinition} is a major part
  14. * of the application. Typically corresponds to a user-defined bean.
  15. */
  16. int ROLE_APPLICATION = 0;
  17. /**
  18. * Role hint indicating that a {@code BeanDefinition} is a supporting
  19. * part of some larger configuration, typically an outer
  20. * {@link org.springframework.beans.factory.parsing.ComponentDefinition}.
  21. * {@code SUPPORT} beans are considered important enough to be aware
  22. * of when looking more closely at a particular
  23. * {@link org.springframework.beans.factory.parsing.ComponentDefinition},
  24. * but not when looking at the overall configuration of an application.
  25. */
  26. int ROLE_SUPPORT = 1;
  27. /**
  28. * Role hint indicating that a {@code BeanDefinition} is providing an
  29. * entirely background role and has no relevance to the end-user. This hint is
  30. * used when registering beans that are completely part of the internal workings
  31. * of a {@link org.springframework.beans.factory.parsing.ComponentDefinition}.
  32. */
  33. int ROLE_INFRASTRUCTURE = 2;
  34. /**
  35. * Return the name of the parent definition of this bean definition, if any.
  36. */
  37. String getParentName();
  38. /**
  39. * Set the name of the parent definition of this bean definition, if any.
  40. */
  41. void setParentName(String parentName);
  42. /**
  43. * Return the current bean class name of this bean definition.
  44. * <p>Note that this does not have to be the actual class name used at runtime, in
  45. * case of a child definition overriding/inheriting the class name from its parent.
  46. * Hence, do <i>not</i> consider this to be the definitive bean type at runtime but
  47. * rather only use it for parsing purposes at the individual bean definition level.
  48. */
  49. String getBeanClassName();
  50. /**
  51. * Override the bean class name of this bean definition.
  52. * <p>The class name can be modified during bean factory post-processing,
  53. * typically replacing the original class name with a parsed variant of it.
  54. */
  55. void setBeanClassName(String beanClassName);
  56. /**
  57. * Return the factory bean name, if any.
  58. */
  59. String getFactoryBeanName();
  60. /**
  61. * Specify the factory bean to use, if any.
  62. */
  63. void setFactoryBeanName(String factoryBeanName);
  64. /**
  65. * Return a factory method, if any.
  66. */
  67. String getFactoryMethodName();
  68. /**
  69. * Specify a factory method, if any. This method will be invoked with
  70. * constructor arguments, or with no arguments if none are specified.
  71. * The method will be invoked on the specified factory bean, if any,
  72. * or otherwise as a static method on the local bean class.
  73. * @param factoryMethodName static factory method name,
  74. * or {@code null} if normal constructor creation should be used
  75. */
  76. void setFactoryMethodName(String factoryMethodName);
  77. /**
  78. * Return the name of the current target scope for this bean,
  79. * or {@code null} if not known yet.
  80. */
  81. String getScope();
  82. /**
  83. * Override the target scope of this bean, specifying a new scope name.
  84. */
  85. void setScope(String scope);
  86. /**
  87. * Return whether this bean should be lazily initialized, i.e. not
  88. * eagerly instantiated on startup. Only applicable to a singleton bean.
  89. */
  90. boolean isLazyInit();
  91. /**
  92. * Set whether this bean should be lazily initialized.
  93. * <p>If {@code false}, the bean will get instantiated on startup by bean
  94. * factories that perform eager initialization of singletons.
  95. */
  96. void setLazyInit(boolean lazyInit);
  97. /**
  98. * Return the bean names that this bean depends on.
  99. */
  100. String[] getDependsOn();
  101. /**
  102. * Set the names of the beans that this bean depends on being initialized.
  103. * The bean factory will guarantee that these beans get initialized first.
  104. */
  105. void setDependsOn(String... dependsOn);
  106. /**
  107. * Return whether this bean is a candidate for getting autowired into some other bean.
  108. */
  109. boolean isAutowireCandidate();
  110. /**
  111. * Set whether this bean is a candidate for getting autowired into some other bean.
  112. */
  113. void setAutowireCandidate(boolean autowireCandidate);
  114. /**
  115. * Return whether this bean is a primary autowire candidate.
  116. * If this value is true for exactly one bean among multiple
  117. * matching candidates, it will serve as a tie-breaker.
  118. */
  119. boolean isPrimary();
  120. /**
  121. * Set whether this bean is a primary autowire candidate.
  122. * <p>If this value is true for exactly one bean among multiple
  123. * matching candidates, it will serve as a tie-breaker.
  124. */
  125. void setPrimary(boolean primary);
  126. /**
  127. * Return the constructor argument values for this bean.
  128. * <p>The returned instance can be modified during bean factory post-processing.
  129. * @return the ConstructorArgumentValues object (never {@code null})
  130. */
  131. ConstructorArgumentValues getConstructorArgumentValues();
  132. /**
  133. * Return the property values to be applied to a new instance of the bean.
  134. * <p>The returned instance can be modified during bean factory post-processing.
  135. * @return the MutablePropertyValues object (never {@code null})
  136. */
  137. MutablePropertyValues getPropertyValues();
  138. /**
  139. * Return whether this a <b>Singleton</b>, with a single, shared instance
  140. * returned on all calls.
  141. */
  142. boolean isSingleton();
  143. /**
  144. * Return whether this a <b>Prototype</b>, with an independent instance
  145. * returned for each call.
  146. */
  147. boolean isPrototype();
  148. /**
  149. * Return whether this bean is "abstract", that is, not meant to be instantiated.
  150. */
  151. boolean isAbstract();
  152. /**
  153. * Get the role hint for this {@code BeanDefinition}. The role hint
  154. * provides the frameworks as well as tools with an indication of
  155. * the role and importance of a particular {@code BeanDefinition}.
  156. */
  157. int getRole();
  158. /**
  159. * Return a human-readable description of this bean definition.
  160. */
  161. String getDescription();
  162. /**
  163. * Return a description of the resource that this bean definition
  164. * came from (for the purpose of showing context in case of errors).
  165. */
  166. String getResourceDescription();
  167. /**
  168. * Return the originating BeanDefinition, or {@code null} if none.
  169. * Allows for retrieving the decorated bean definition, if any.
  170. * <p>Note that this method returns the immediate originator. Iterate through the
  171. * originator chain to find the original BeanDefinition as defined by the user.
  172. */
  173. BeanDefinition getOriginatingBeanDefinition();
  174. }

可以看到上面的很多属性和方法都很熟悉,例如类名、scope、属性、构造函数参数列表、依赖的bean、是否是单例类、是否是懒加载等,其实就是将Bean的定义信息存储到这个BeanDefinition相应的属性中,后面对Bean的操作就直接对BeanDefinition进行,例如拿到这个BeanDefinition后,可以根据里面的类名、构造函数、构造函数参数,使用反射进行对象创建。BeanDefinition是一个接口,是一个抽象的定义,实际使用的是其实现类,如ChildBeanDefinition、RootBeanDefinition、GenericBeanDefinition等。BeanDefinition继承了AttributeAccessor,说明它具有处理属性的能力;BeanDefinition继承了BeanMetadataElement,说明它可以持有Bean元数据元素,作用是可以持有XML文件的一个bean标签对应的Object。

概括的描述一下Spring注册流程的更多相关文章

  1. Spring(六)Spring执行流程

    Spring MVC工作流程图 Spring工作流程描述 1. 用户向服务器发送请求,请求被Spring 前端控制Servelt DispatcherServlet捕获: 2. DispatcherS ...

  2. Java框架之SpringMVC 05-拦截器-异常映射-Spring工作流程

    SpringMVC 拦截器 Spring MVC也可以使用拦截器对请求进行拦截处理,可以自定义拦截器来实现特定的功能,自定义的拦截器可以实现HandlerInterceptor接口中的三个方法,也可以 ...

  3. 从注册流程 分析如何安全退出多个Activity 多种方式(附DEMO)

      退出Activity注册Android遍历   目录(?)[+] 前言 知识结构 具体方案 方案1 方法采用FLAG_ACTIVITY_CLEAR_TOP退出整个程序多activity 方案2 方 ...

  4. 基于GBT28181:SIP协议组件开发-----------第三篇SIP注册流程分析实现

    原创文章,引用请保证原文完整性,尊重作者劳动,原文地址http://www.cnblogs.com/qq1269122125/p/3941172.html,qq:1269122125. 上两章节简要的 ...

  5. 从注册流程 分析如何安全退出多个Activity 多种方式(附DEMO)

    前言   由于一个同学问到我如何按照一个流程走好之后回到首页,我以前看到过4个解决方案,后来发现有做个记录和总结的必要,就写了这篇博文.(之前看小强也写过一篇,这里通过自身的分析完整的总结一下以下6种 ...

  6. sip 注册流程

    基本注册流程示意图: 注册流程描述如下: 1.         SIP代理向SIP服务器发送REGISTER请求: 2.         SIP服务器向SIP代理发送响应401,并在响应的消息头WWW ...

  7. 微信小程序注册流程

    响应公司号召,跟上时代潮流,接下来我将独自开发微信小程序,接下来我介绍下注册流程,后续会补上小程序开发心得. 注册流程 注册之前,需要使用一个邮箱,该邮箱作为登录小程序的账号,这个邮箱不能被微信开放平 ...

  8. wifi万能钥匙自媒体平台开放注册(付注册流程)

    12月13日,有网友爆料,wifi万能钥匙自媒体开放注册,看来自媒体还没有达到饱和阶段,也印证了自媒体时代才刚刚到来.现在这个自媒体的时代,几乎大多互联网企业都开通了自己的自媒体,比较知名的像今日头条 ...

  9. 源程序版本管理软件和项目管理软件,Github注册流程

    目前流行的源程序版本管理软件和项目管理软件:Microsoft TFS,Github,SVN,Coding 各自的优缺点: Microsoft TFS: 优点: tfs核心的,是对敏捷,msf,cmm ...

随机推荐

  1. mysql导入文件

    手里有一个web源码工程文件夹 mysql导入文件: 新建连接,名称随意,用修改设置的用户密码登录,我的连接名称是eee 右击information_schema,建立数据库,数据库名称源码文件名,字 ...

  2. Codeforces Round #563 (Div. 2) B. Ehab Is an Odd Person

    链接:https://codeforces.com/contest/1174/problem/B 题意: You're given an array aa of length nn. You can ...

  3. Magic Maze dfs + dp

    http://swjtuoj.cn/problem/2387/ 设dp[cur]表示以cur这个节点为起点的时候,能走的最大贡献. 题目保证没环,也就是没回路. 感觉和树dp差不多了. #includ ...

  4. 项目协作管理平台-teambition和tapd--深度体验

    ​ 一.分析目的 通过分析2B产品中的团队协作管理软件的对比分析,用于为公司团队协作软件的选型做产考. 二.竞品归属市场概况 2.1.目标用户群及需求 主要面向企业用户,用于解决企业不同地域以及不同职 ...

  5. springMVC框架的理解加深,个人的一些想法

    一 写spring-boot整合的时候,有种想看源码的冲动!呸,是钻牛角尖的毛病犯了... @RequestMapping("/index") public String inde ...

  6. 省厅报件7.0 读取mdb 生成xml 文件

    using System;using System.Collections.Generic;using System.Data;using System.Data.OleDb;using System ...

  7. h5点击区域和实际区域对不上

    点击区域和实际区域对不上 然后点击后触发的其实是上面的区域,会导致事件触发错误

  8. ASPCTJ

    ublic interface ISomeService { public void doSome(); public String dade(); } public class SomeServic ...

  9. Redis set(集合)

    Redis 的 Set 是 String 类型的无序集合,元素不允许重复. Redis 中集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是 O(1). 集合中最大的元素数为 232 - 1 ( ...

  10. 韦东山笔记之安装arm-linux-gcc交叉编译环境详细步骤。

    1在关盘主目录tools下复制arm-linux-gcc-3.4.5-glibc-2.3.6.tar.bz2到虚拟机上     解压 tar xjf arm-linux-gcc-3.4.5-glibc ...