免费预览已结束,剩余1页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
2.2 IoC容器系列的实现:BeanFactory和ApplicationContext2.2.1 BeanFactory对IoC容器的功能定义从前面的介绍,我们知道BeanFactory定义了IoC容器的基本功能规范,所以,下面我们就从BeanFactory这个最基本的容器定义来进入Spring的IoC容器体系,去了解IoC容器的实现原理。IoC容器的基本接口是由BeanFactory来定义的,也就是说,BeanFactory定义了IoC容器的最基本的形式,并且提供了IoC容器所应该遵守的最基本的服务契约。同时,这也是我们使用IoC容器所应遵守的最底层和最基本的编程规范,这些接口定义勾画出了IoC的基本轮廓。很显然,在Spring的代码实现中,BeanFactory只是一个接口类,并没有给出容器的具体实现,而我们在图2-2中看到的各种具体类,比如DefaultListableBeanFactory、XmlBeanFactory、ApplicationContext等都可以看成是容器的附加了某种功能的具体实现,也就是容器体系中的具体容器产品。下面我们来看看BeanFactory是怎样定义IoC容器的基本接口的。下面介绍这个基本接口为用户提供的基本功能。用户使用容器时,可以使用转义符&来得到FactoryBean本身,用来区分通过容器来获取FactoryBean产生的对象和获取FactoryBean本身。举例来说,如果myJndiObject是一个FactoryBean,那么使用&myJndiObject得到的是FactoryBean,而不是myJndiObject这个FactoryBean产生出来的对象。注意 理解上面这段话需要很好地区分FactoryBean和BeanFactory这两个在Spring中使用频率很高的类,它们在拼写上非常相似。一个是Factory,也就是IoC容器或对象工厂;一个是Bean。在Spring中,所有Bean都是由BeanFactory(也就是IoC容器)来进行管理的。但对FactoryBean而言,这个Bean不是简单的Bean,而是一个能产生或者修饰对象生成的工厂Bean,它的实现与设计模式中的工厂模式和修饰器模式类似。BeanFactory接口设计了getBean方法,这个方法是使用IoC容器API的主要方法,通过这个方法,可以取得IoC容器中管理的Bean,Bean的取得是通过指定名字来进行索引的。如果需要在获取Bean时对Bean的类型进行检查,BeanFactory接口定义了带有参数的getBean方法,这个方法的使用与getBean方法类似,不同的是增加了对Bean检索的类型的要求。用户可以通过BeanFactory接口方法getBean来使用Bean名字,从而当获取Bean时,如果需要获取的Bean是prototype类型的,用户还可以为这个prototype类型的Bean生成指定构造函数的对应参数。这使得在一定程度上可以控制生成prototype类型的Bean。有了BeanFactory的定义,用户可以执行以下操作:* 通过接口方法containsBean让用户能够判断容器是否含有指定名字的Bean。* 通过接口方法isSingleton来查询指定了名字的Bean是否是Singleton类型的Bean。对于Singleton属性,用户可以在BeanDefinition中指定。* 通过接口方法isPrototype来查询指定了名字的Bean是否是prototype类型的。与Singleton属性一样,这个属性也可以由用户在BeanDefinition中指定。* 通过接口方法isTypeMatch来查询指定了名字的Bean的Class类型是否是特定的Class类型。这个Class类型可以由用户来指定。* 通过接口方法getType来查询指定了名字的Bean的Class类型。* 通过接口方法getAliases来查询指定了名字的Bean的所有别名,这些别名都是用户在BeanDefinition中定义的。这些定义的接口方法勾画出了IoC容器的基本特性,因为BeanFactory接口定义了IoC容器,所以下面给出它定义的全部内容来让大家参考,如代码清单2-1所示。代码清单2-1 BeanFactory接口1. publicinterfaceBeanFactory 2. /* 3. *UsedtodereferencealinkFactoryBean 4. instanceanddistinguishitfrom 5. *beanscreatedbytheFactoryBean.For 6. example,ifthebeannamed 7. *myJndiObjectisaFactoryBean, 8. getting&myJndiObject 9. *willreturnthefactory,nottheinstance 10. returnedbythefactory. 11. */12. StringFACTORY_BEAN_PREFIX=&; 13. /* 14. *Returnaninstance,whichmaybesharedor 15. independent,ofthespecifiedbean. 16. *ThismethodallowsaSpringBeanFactorytobeusedas 17. areplacementforthe 18. *SingletonorPrototypedesignpattern.Callersmayretain 19. referencesto 20. *returnedobjectsinthecaseofSingletonbeans. 21. *Translatesaliasesbacktothecorrespondingcanonical 22. beanname. 23. *Willasktheparentfactoryifthebeancannotbefound 24. inthisfactoryinstance. 25. */26. ObjectgetBean(Stringname)throwsBeansException; 27. /* 28. *Returnaninstance,whichmaybesharedor 29. independent,ofthespecifiedbean. 30. *Behavesthesameaslink#getBean(String),but 31. providesameasureoftype 32. *safetybythrowingaBeanNotOfRequiredTypeExceptionif 33. thebeanisnotofthe 34. *requiredtype.ThismeansthatClassCastExceptioncant 35. bethrownoncasting 36. *theresultcorrectly,ascanhappenwithlink 37. #getBean(String). 38. *Translatesaliasesbacktothecorrespondingcanonical 39. beanname. 40. *Willasktheparentfactoryifthebeancannotbe 41. foundinthisfactoryinstance. 42. */43. TgetBean(Stringname,ClassrequiredType) 44. throwsBeansException; 45. /* 46. *Returnaninstance,whichmaybesharedor 47. independent,ofthespecifiedbean. 48. *Allowsforspecifyingexplicitconstructor 49. arguments/factorymethodarguments, 50. *overridingthespecifieddefaultarguments(ifany)in 51. thebeandefinition. 52. */53. ObjectgetBean(Stringname,Object.args)throws54. BeansException; 55. /* 56. *Doesthisbeanfactorycontainabeanwiththe 57. givenname?Morespecifically, 58. *islink#getBeanabletoobtainabean 59. instanceforthegivenname? 60. *Translatesaliasesbacktothecorresponding 61. canonicalbeanname. 62. *Willasktheparentfactoryifthebeancannotbe 63. foundinthisfactoryinstance. 64. */65. booleancontainsBean(Stringname); 66. /* 67. *Isthisbeanasharedsingleton?Thatis,will 68. link#getBeanalways 69. *returnthesameinstance? 70. *Note:Thismethodreturningfalse 71. doesnotclearlyindicate 72. *independentinstances.Itindicatesnon-singleton 73. instances,whichmaycorrespond 74. *toascopedbeanaswell.Usethelink 75. #isPrototypeoperationtoexplicitly 76. *checkforindependentinstances. 77. *Translatesaliasesbacktothecorrespondingcanonical 78. beanname. 79. *Willasktheparentfactoryifthebeancannotbefound 80. inthisfactoryinstance. 81. */82. booleanisSingleton(Stringname)throws83. NoSuchBeanDefinitionException; 84. /* 85. *Isthisbeanaprototype?Thatis,willlink#getBean 86. alwaysreturn 87. *independentinstances? 88. *Note:Thismethodreturningfalsedoes 89. notclearlyindicate 90. *asingletonobject.Itindicatesnon-independent 91. instances,whichmaycorrespond 92. *toascopedbeanaswell.Usethelink#isSingleton 93. operationtoexplicitly 94. *checkforasharedsingletoninstance. 95. *Translatesaliasesbacktothecorresponding 96. canonicalbeanname. 97. *Willasktheparentfactoryifthebeancannotbefound 98. inthisfactoryinstance. 99. */100. booleanisPrototype(Stringname)throws101. NoSuchBeanDefinitionException; 102. /* 103. *Checkwhetherthebeanwiththegivennamematchesthe 104. specifiedtype. 105. *Morespecifically,checkwhetheralink#getBeancall 106. forthegivenname 107. *wouldreturnanobjectthatisassignabletothespecified 108. targettype. 109. *Translatesaliasesbacktothecorrespondingcanonical 110. beanname. 111. *Willasktheparentfactoryifthebeancannotbefoundin 112. thisfactoryinstance. 113. */114. booleanisTypeMatch(Stringname,ClasstargetType) 115. throwsNoSuchBeanDefinitionException; 116. /* 117. *Determinethetypeofthebeanwiththegivenname.More 118. specifically, 119. *determinethetypeofobjectthatlink#getBeanwould 120. returnforthegivenname. 121. *ForalinkFactoryBean,returnthetypeofobject 122. thattheFactoryBeancreates, 123. *asexposedbylinkFactoryBean#getObjectType(). 124. *Translatesalias
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 七年级道德与法治下册第三单元第七课弘扬中华人文精神教学设计
- 小学五年级数学“新学期活动中的八折问题”教学设计
- 小学一年级劳动教育土培蒜苗种植活动教学设计
- 小学四年级科学《凹陷的保鲜膜》教学设计
- 初中八年级英语不定代词专题复习与提优教学设计
- 初中心理健康《在竞争中双赢》教学设计
- 2026事业单位工勤技能-新疆-新疆地图绘制员四级(中级工)历年参考题库含答案详解
- CN119538102A 柴油发电机组故障预测方法、模型、及储存介质 (深圳市怡昌动力技术有限公司)
- CN119537852A 一种光谱驱动和贪心优化集成模型的土壤性质定量分析方法 (皖西学院)
- CN119537414A 一种基于大模型的电力检修数据的结构化提取方法及系统 (中国科学院沈阳计算技术研究所有限公司)
- 油漆店面应急预案
- 2025年四川省内江市专业技术继续教育公需科目考试及答案
- 戒毒所生产安全知识培训课件
- 工程合同外增加项补充协议模板
- 风电设备典型故障诊断处理指导手册第3册2025
- 2025年陕西陕煤集团榆林化学有限责任公司招聘笔试参考题库含答案解析
- 2025年国信证券股份有限公司招聘笔试参考题库含答案解析
- 铁路电子客票应急处置
- 中建厂房钢结构专项施工方案
- 2024年冬季N0M世界青少年数学思维研学活动模拟题4年级
- QC/T 985-2024汽车热反射镀膜夹层前风窗玻璃
评论
0/150
提交评论