注入2.4 IoC容器的依赖注入(5).

依赖注入  时间:2021-02-08  阅读:()

代码清单2-301.private vo id setPropertyValue(PropertyTokenHo lder tokens,

2.PropertyValue pv) throws BeansException{String

3.propertyName=tokens.canonicalName;S tring actualName=

4. tokens.actualName; if(tokens.keys !=null) { //App ly

5. indexes and map keys:fetch value for all keys but the last

6.one.PropertyTokenHo lder getterTokens=new

7.PropertyTokenHo lder();getterTokens.cano nicalName=

8. tokens.cano nicalName;getterTokens.actualName=

9. tokens.actualName;getterTokens.keys=new

10.String[tokens.keys.length- 1];

11.Sys tem.arraycop y(tokens.keys,0,getterTokens.keys,0,

12. tokens.keys. length- 1);Obje ct propValue;

13. //getP ropertyValue取得bean中注入对象的引用 比如Array、 List

14. 、Map、 Set等。 try{propValue=

15.ge tPropertyVa lue(ge tte rTok e ns); } catc h

16. (NotReadablePropertyException ex) { throw new

17.NotWritab lePropertyException(getRootC lass(), this.nested

18.Path+propertyName, "Cannot access indexed value in

19.property referenced"+"in indexed property path'"+

20.propertyName+"'", ex); } //Set value for last key.

21.String key=tokens.keys[tokens.keys.length- 1]; if

22. (propValue==null) { throw new

23.NullValueInNestedPathException(getRootC las s(), this.ne sted

24.Path+propertyName, "Cannot access indexed value in

25.property referenced"+"in indexed property path'"+

26.propertyName+"' :returned null"); }

27. //这里对Array进行注入。 else if

28. (propValue.getC lass().isArray()) {C lass requiredType=

29.propValue.getC la ss().getCo mpo nentType(); int arrayInde x=

30. Inte ger.parseInt(key);Object o ldValue=null; try{

31. if(is Extra ctO ldVa lueForEd ito r()) {o ldVa lue=

32.Array.ge t(p ropVa lue,arrayInde x); }Obj ect

33.convertedValue=this.typeCo nverterDe le gate.

34.convertI fNec essary(propertyName,o ldValue,p v.getValue(),

35.required Type);Array.set(p ropVa lue,

36. Inte ger.p a rs eI nt(ke y),c o nvertedVa lue); } c atc h

37. (IllegalArgumentException ex) {Prop ertyChangeEvent pce

38.=new

39.PropertyC hangeEvent(this.rootObject,this.nestedPath+

40.propertyName,o ldValue,p v.ge tValue()); thro w new

41.TypeM ismatchExceptio n(pce, requiredTyp e,e x); } catch

42. (IllegalStateException ex) {PropertyChangeEvent pce=

43.new PropertyChangeEvent(this.rootObject,this.nestedPath

44.+propertyName,o ldValue,p v.getValue()); thro w new

45.Convers ionNotS upportedException(pce,requiredType,ex); }

46.catch(IndexOutOfBoundsException ex) { throw new

47. Inva lidP ropertyExcep tio n(getRootC lass(),this.nes tedPath+

48.propertyName,"Invalid array index in property path'"+

49.propertyName+"'", ex); } } //这里对List进行注入。

50.else if(propValue instanceof List) {PropertyDescriptor

51.pd=getCachedIntrospectio nResults().getP ropertyDes crip tor

52. (actualName);C lass requiredType=

53.GenericCo llectionTypeReso lver.getCo llectionReturnType(

54.pd.getReadMethod(), tokens.keys.length);List list=

55. (List)propValue; int index=Inte ger.p arse Int(key);

56.Obj ect o ldVa lue=null; if(isExtrac tO ldVa lueForEd itor()

57.&&index<list.s ize()) {o ldValue=list.get(index);

58. } try{Object convertedValue=

59. this.typeConverterDe le gate.convertI f Neces sary(

60.propertyName,o ldVa lue,p v.ge tVa lue(),required Type); if

61. (index<list.s ize()) { list.set(index,

62.convertedValue); } else if(index>=list.size()) {

63.for(int i=list.size(); i<index; i++) { try{

64. lis t.add(null); } catc h(N ullPo interExcep tio n e x) {

65. throw new InvalidP ropertyExcep tio n(getRootC lass(),

66. this.nestedPath+propertyName, "Cannot set element

67.with index"+index+" in List of size "+

68. list.size()+",accessed using property path'"+

69.propertyName+"' :List does not support filling up

70.gaps with null elements"); } }

71. lis t.add(co nve rted Va lue); } } catc h

72. (IllegalArgumentException ex) {Prop ertyChangeEvent pce=

73.new PropertyChangeEvent(this.rootObje ct,this.nestedPath

74.+propertyName,o ldValue,p v.getValue()); thro w new

75.TypeM ismatchExceptio n(pce, requiredTyp e,ex); } }

76. //里对Map进行注入。 else if(propValue instanceofMap) {

77.PropertyDescriptor pd=

78.getCachedIntrospectionResults().getP ropertyDes crip tor(actual

79.Name);Class mapKeyType=

80.GenericCollectionTypeResolver.getMapKeyReturnType(

81.pd.getReadMethod(), tokens.keys.length);C las s

82.mapValue Type=

83.GenericCollectionTypeResolver.getMapValueReturnType(

84.pd.getReadMethod(), tokens.keys.length);Map map=(Map)

85.propValue;Object convertedMapKey;Object

86.convertedMapValue; try{ /** *IMPORTANT:Do not

87.pass full property name in here-property editors*

88.must not kick in for map keys but rather only for map

89.values. */convertedMapKey=

90. this.typeConverterDe le gate.convertI fNeces sary(key,

91.mapKeyType); } catch(Ille galArgumentExcep tion ex) {

92.PropertyChangeEvent pce=new

93.PropertyC hangeEvent(this.rootObject, this.nestedPath+

94.propertyName,null,p v.ge tValue()); thro w ne w

95.TypeMismatchException(pce,mapKeyType, ex); }Object

96.oldVa lue=null; if(is ExtractO ldVa lueFo rEd ito r()) {

97.oldValue=map.get(convertedMapKey); } try{ /**

98. *Pass full property name and old value in here, since we

99.want full*convers ion ab ility for map value s. */ 100.convertedM apValue=

101. this.typeConverterDe le gate.convertIfNeces sary(

102.propertyName,o ldVa lue,p v.ge tVa lue(),map Va lue Type,null, 103.ne wMethodParamete r(pd.getReadMethod(), -1, tokens. 104.keys.length+1)); } catc h(IllegalArgumentException 105.ex) {PropertyChangeEvent pce=new

106.PropertyC hangeEvent(this.rootObject, this.nested Path+

107.propertyName,o ldValue,p v.ge tValue()); thro w new

108.TypeMismatchException(pce,mapValue Type,ex); }

109.map.put(convertedMapKey, convertedMapValue); } els e { 110. throw newInva lidP ropertyExcep tio n(getRootC lass(),

111. this.nestedPath+propertyName, "Property re ferenced 112. in indexed propertypath'"+propertyName+"' is

113.neither an array nor a List nor a Map;returned value was [" 114.+pv.getValue()+"]"); } }//这里对非集合类的域进行注入。

115.else {PropertyDe scriptor pd=pv.reso lvedDes criptor; if 116. (pd==null| |

117. !pd.getWriteMethod().getDec laringC lass().isInstance

118. (this.object)) {pd=

119.getCachedIntrospectionResults().getP ropertyDes criptor(actual 120.Name); if(pd==null | |pd.ge tWrite Method()==null) { 121.Prope rtyMatc hes matc he s

122.=PropertyM atches.forProperty(propertyName,

123.getRootC lass()); throw new NotWritab lePropertyException( 124.getRootC las s(),this.nestedPath+propertyName,

125.matc hes.b uildErrorMe ssa ge(),matc hes.ge tPo ss ib leM atc hes()); 126. }pv.getOriginalPropertyValue().re so lvedDescripto r=pd; 127. }Object o ldValue=null;try{Object originalValue 128.=pv.getValue();Object valueToApply=originalValue;

129. if(!Boolean.FALSE.equals(pv.conversionNecessary)) { if 130. (pv.isConverted()){va lue To App ly=

131.pv.ge tC onve rtedVa lue(); } e lse { if

132. (is ExtractO ldVa lueFo rEd ito r()&&pd.ge tReadMe thod() !=null) 133. {MethodreadMethod=pd.getReadMethod(); if

134. (!Modifier.isPub lic(readMethod.getDec laringC lass().

135.getModifiers())) {readM ethod.setAccess ib le(true); } 136. try{o ldValue=readMethod.invoke(this.object); 137. } catch(Exception ex) { if

138. (logger.isDebugEnabled()) { logger.debug("Could not 139. read previous value ofproperty'"+

140. this.nestedPath+propertyName+"'",ex); } } 141. }valueToApp ly=

142. this.typeConverterDe le gate.convertIfNeces sary

143. (o ldValue,originalValue,pd); }

144.pv.getOriginalPrope rtyValue().co nvers io nNeces sary=

145. (value ToApp ly!=originalValue); }

146. //这里取得注入属性的set方法通过反射机制把对象注入进去。

147.Method writeMethod=pd.getWriteMethod(); if

148. (!Modifier.isPublic(writeMethod.getDeclaringC lass().getModif 149. iers())){writeMethod.setAccessib le(true); }

150.writeMethod.invoke(this.object,valueToApp ly); } catch

151. (InvocationTargetExc eption ex) {PropertyChangeEvent

152.propertyC hangeEvent=new

153.PropertyC hangeEvent(this.rootObject, this.nestedPath+

154.propertyName,o ldValue,p v.getValue()); if

155. (ex.getTargetException() instanceof C lassCastException) {

156.pd.getPropertyTyp e(),ex.getTargetException()); } else

157. { throw new

158.MethodInvocationException(propertyChangeEvent,

159.ex.ge tT argetExcep tio n()); } } catc h

160. (IllegalArgumentException ex) {Prop ertyChangeEvent pce=161.new

PropertyC hangeEvent(this.roo tObject, this.nestedPath+162.propertyName,o ldValue,pv.getValue()); thro w new

163.TypeMismatchException(pce,pd.getPropertyType(),ex); }

164.catch(Ille galState Exceptio n ex) {PropertyC hange Event

165.pce=new PropertyChangeEvent(this.rootObject,

166. this.nestedPath+propertyName,o ldValue, 167.p v.getValue()); throw new

168.ConversionNotS upportedException(pce,pd.getPropertyType(),

169.ex); } catch(IllegalAcce ssException ex) {

170.PropertyChangeEvent pce=new

171.PropertyC hangeEvent(this.rootObject, this.nestedPath+

172.propertyName,o ldValue,p v.ge tValue()); thro w new

173.MethodInvocationException(pce,ex); } }}

这样就完成了对各种bean属性的依赖注入过程。笔者以前探寻过Spring 2.0的源代码在这里发现Spring 3.0的源代码已经有了很大的改进整个过程更为清晰了特别是关于依赖注入的这一部分。如果读者有兴趣可以分别研究并比较一下Spring 2.0和Spring 3.0对这部分的实现这样可以更清晰地看到Spring源代码的演进过程也可以看到Spring团队对代码进行重构的思路。

在Bean的创建和对象依赖注入的过程中需要依据BeanDe finition中的信息来递归地完成依赖注入。从上面可以看到几个递归过程这些递归都是以getBean为入口的。一个是在上下文体系中查找需要的Bean和创建Bean的递归调用另一个递归在依赖注入时通过递归调用容器的getBean方法得到当前Bean的依赖Bean 同时也触发对依赖Bean的创建和注入。在对Bean的属性进行依赖注入

时解析的过程也是一个递归的过程。这样根据依赖关系一层一层地完成Bean的创建和注入直到最后完成当前Bean的创建有了这个顶层Bean的创建和对它的属性依赖注入的完成也意味着和当前Bean相关的整个依赖链的注入完成。

在Bean建立和依赖注入完成以后在IoC容器中建立起一系列靠依赖关系联系起来的Bean这个Bean已经不是简单的Java对象了。这个Bean系列建立完成以后通过IoC容器的相关接口方法就可以非常方便地让上层应用使用了。回到前面的关于水桶的例子到这里我们不但找到了水源而且成功地把水装到了水桶中 同时对水桶里的水完成了一系列的处理 比如消毒、煮沸……尽管还是水但经过一系列的处理以后这些水已经是开水了 已经可以直接饮用了

美国cera机房 2核4G 19.9元/月 宿主机 E5 2696v2x2 512G

美国特价云服务器 2核4G 19.9元杭州王小玉网络科技有限公司成立于2020是拥有IDC ISP资质的正规公司,这次推荐的美国云服务器也是商家主打产品,有点在于稳定 速度 数据安全。企业级数据安全保障,支持异地灾备,数据安全系数达到了100%安全级别,是国内唯一一家美国云服务器拥有这个安全级别的商家。E5 2696v2x2 2核 4G内存 20G系统盘 10G数据盘 20M带宽 100G流量 1...

DiyVM:香港VPS五折月付50元起,2核/2G内存/50G硬盘/2M带宽/CN2线路

diyvm怎么样?diyvm这是一家低调国人VPS主机商,成立于2009年,提供的产品包括VPS主机和独立服务器租用等,数据中心包括香港沙田、美国洛杉矶、日本大阪等,VPS主机基于XEN架构,均为国内直连线路,主机支持异地备份与自定义镜像,可提供内网IP。最近,DiyVM商家对香港机房VPS提供5折优惠码,最低2GB内存起优惠后仅需50元/月。点击进入:diyvm官方网站地址DiyVM香港机房CN...

ZJI:韩国BGP+CN2线路服务器,国内三网访问速度优秀,8折优惠码每月实付440元起

zji怎么样?zji最近新上韩国BGP+CN2线路服务器,国内三网访问速度优秀,适用8折优惠码zji,优惠后韩国服务器最低每月440元起。zji主机支持安装Linux或者Windows操作系统,会员中心集成电源管理功能,8折优惠码为终身折扣,续费同价,全场适用。ZJI是原Wordpress圈知名主机商:维翔主机,成立于2011年,2018年9月启用新域名ZJI,提供中国香港、台湾、日本、美国独立服...

依赖注入为你推荐
主页改不了怎么改不了主页windows优化大师怎么用windows优化大师怎么用﹖站长故事爱迪生发明东西的故事显卡温度多少正常显卡温度多少算正常网店推广网站网店怎么推广?腾讯文章怎样才能在手机腾讯网上发表文章?开机滚动条电脑开机滚动条要走好几次创维云电视功能很喜欢创维云电视,它到底有哪些独特功能?商标注册查询官网如何在网上查询商标是否注册?如何快速收录谁知道怎么快速被搜索引擎快速收录啊?
联通vps 老域名全部失效请记好新域名 winscp 老左博客 unsplash 华为云主机 申请空间 52测评网 有益网络 php空间推荐 最好的qq空间 华为云盘 免费外链相册 新加坡空间 云服务器比较 阿里云邮箱登陆地址 免备案cdn加速 alexa世界排名 服务器机柜 asp.net虚拟主机 更多