2.4 IoC容器的依赖注入(5).docx_第1页
2.4 IoC容器的依赖注入(5).docx_第2页
2.4 IoC容器的依赖注入(5).docx_第3页
2.4 IoC容器的依赖注入(5).docx_第4页
2.4 IoC容器的依赖注入(5).docx_第5页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

代码清单2-301. privatevoidsetPropertyValue(PropertyTokenHoldertokens, 2. PropertyValuepv)throwsBeansExceptionString 3. propertyName=tokens.canonicalName;StringactualName= 4. tokens.actualName;if(tokens.keys!=null)/Apply 5. indexesandmapkeys:fetchvalueforallkeysbutthelast 6. one.PropertyTokenHoldergetterTokens=new7. PropertyTokenHolder();getterTokens.canonicalName= 8. tokens.canonicalName;getterTokens.actualName= 9. tokens.actualName;getterTokens.keys=new10. Stringtokens.keys.length-1; 11. System.arraycopy(tokens.keys,0,getterTokens.keys,0, 12. tokens.keys.length-1);ObjectpropValue; 13. /getPropertyValue取得bean中注入对象的引用,比如Array、List 14. 、Map、Set等。trypropValue= 15. getPropertyValue(getterTokens);catch16. (NotReadablePropertyExceptionex)thrownew17. NotWritablePropertyException(getRootClass(),this.nested 18. Path+propertyName,Cannotaccessindexedvaluein 19. propertyreferenced+inindexedpropertypath+ 20. propertyName+,ex);/Setvalueforlastkey. 21. Stringkey=tokens.keystokens.keys.length-1;if22. (propValue=null)thrownew23. NullValueInNestedPathException(getRootClass(),this.nested 24. Path+propertyName,Cannotaccessindexedvaluein 25. propertyreferenced+inindexedpropertypath+ 26. propertyName+:returnednull); 27. /这里对Array进行注入。elseif 28. (propValue.getClass().isArray()ClassrequiredType= 29. propValue.getClass().getComponentType();intarrayIndex= 30. Integer.parseInt(key);ObjectoldValue=null;try 31. if(isExtractOldValueForEditor()oldValue= 32. Array.get(propValue,arrayIndex);Object 33. convertedValue=this.typeConverterDelegate. 34. convertIfNecessary(propertyName,oldValue,pv.getValue(), 35. requiredType);Array.set(propValue, 36. Integer.parseInt(key),convertedValue);catch37. (IllegalArgumentExceptionex)PropertyChangeEventpce 38. =new39. PropertyChangeEvent(this.rootObject,this.nestedPath+ 40. propertyName,oldValue,pv.getValue();thrownew41. TypeMismatchException(pce,requiredType,ex);catch42. (IllegalStateExceptionex)PropertyChangeEventpce= 43. newPropertyChangeEvent(this.rootObject,this.nestedPath 44. +propertyName,oldValue,pv.getValue();thrownew45. ConversionNotSupportedException(pce,requiredType,ex); 46. catch(IndexOutOfBoundsExceptionex)thrownew47. InvalidPropertyException(getRootClass(),this.nestedPath+ 48. propertyName,Invalidarrayindexinpropertypath+ 49. propertyName+,ex);/这里对List进行注入。 50. elseif(propValueinstanceofList)PropertyDescriptor 51. pd=getCachedIntrospectionResults().getPropertyDescriptor 52. (actualName);ClassrequiredType= 53. GenericCollectionTypeResolver.getCollectionReturnType( 54. pd.getReadMethod(),tokens.keys.length);Listlist= 55. (List)propValue;intindex=Integer.parseInt(key); 56. ObjectoldValue=null;if(isExtractOldValueForEditor() 57. &indexlist.size()oldValue=list.get(index); 58. tryObjectconvertedValue= 59. this.typeConverterDelegate.convertIfNecessary( 60. propertyName,oldValue,pv.getValue(),requiredType);if61. (index=list.size() 63. for(inti=list.size();iindex;i+)try 64. list.add(null);catch(NullPointerExceptionex) 65. thrownewInvalidPropertyException(getRootClass(), 66. this.nestedPath+propertyName,Cannotsetelement 67. withindex+index+inListofsize+ 68. list.size()+,accessedusingpropertypath+ 69. propertyName+:Listdoesnotsupportfillingup 70. gapswithnullelements); 71. list.add(convertedValue);catch72. (IllegalArgumentExceptionex)PropertyChangeEventpce= 73. newPropertyChangeEvent(this.rootObject,this.nestedPath 74. +propertyName,oldValue,pv.getValue();thrownew75. TypeMismatchException(pce,requiredType,ex); 76. /里对Map进行注入。elseif(propValueinstanceofMap) 77. PropertyDescriptorpd= 78. getCachedIntrospectionResults().getPropertyDescriptor(actual 79. Name);ClassmapKeyType= 80. GenericCollectionTypeResolver.getMapKeyReturnType( 81. pd.getReadMethod(),tokens.keys.length);Class 82. mapValueType= 83. GenericCollectionTypeResolver.getMapValueReturnType( 84. pd.getReadMethod(),tokens.keys.length);Mapmap=(Map) 85. propValue;ObjectconvertedMapKey;Object 86. convertedMapValue;try/*IMPORTANT:Donot 87. passfullpropertynameinhere-propertyeditors* 88. mustnotkickinformapkeysbutratheronlyformap 89. values.*/convertedMapKey= 90. this.typeConverterDelegate.convertIfNecessary(key, 91. mapKeyType);catch(IllegalArgumentExceptionex) 92. PropertyChangeEventpce=new93. PropertyChangeEvent(this.rootObject,this.nestedPath+ 94. propertyName,null,pv.getValue();thrownew95. TypeMismatchException(pce,mapKeyType,ex);Object 96. oldValue=null;if(isExtractOldValueForEditor() 97. oldValue=map.get(convertedMapKey);try/* 98. *Passfullpropertynameandoldvalueinhere,sincewe 99. wantfull*conversionabilityformapvalues.*/ 100. convertedMapValue= 101. this.typeConverterDelegate.convertIfNecessary( 102. propertyName,oldValue,pv.getValue(),mapValueType,null, 103. newMethodParameter(pd.getReadMethod(),-1,tokens. 104. keys.length+1);catch(IllegalArgumentException 105. ex)PropertyChangeEventpce=new106. PropertyChangeEvent(this.rootObject,this.nestedPath+ 107. propertyName,oldValue,pv.getValue();thrownew108. TypeMismatchException(pce,mapValueType,ex); 109. map.put(convertedMapKey,convertedMapValue);else 110. thrownewInvalidPropertyException(getRootClass(), 111. this.nestedPath+propertyName,Propertyreferenced 112. inindexedpropertypath+propertyName+is 113. neitheranarraynoraListnoraMap;returnedvaluewas 114. +pv.getValue()+);/这里对非集合类的域进行注入。 115. elsePropertyDescriptorpd=pv.resolvedDescriptor;if116. (pd=null| 117. !pd.getWriteMethod().getDeclaringClass().isInstance 118. (this.object)pd= 119. getCachedIntrospectionResults().getPropertyDescriptor(actual 120. Name);if(pd=null|pd.getWriteMethod()=null) 121. PropertyMatchesmatches 122. =PropertyMatches.forProperty(propertyName, 123. getRootClass();thrownewNotWritablePropertyException( 124. getRootClass(),this.nestedPath+propertyName, 125. matches.buildErrorMessage(),matches.getPossibleMatches(); 126. pv.getOriginalPropertyValue().resolvedDescriptor=pd; 127. ObjectoldValue=null;tryObjectoriginalValue 128. =pv.getValue();ObjectvalueToApply=originalValue; 129. if(!Boolean.FALSE.equals(pv.conversionNecessary)if130. (pv.isConverted()valueToApply= 131. pv.getConvertedValue();elseif132. (isExtractOldValueForEditor()&pd.getReadMethod()!=null) 133. MethodreadMethod=pd.getReadMethod();if134. (!Modifier.isPublic(readMethod.getDeclaringClass(). 135. getModifiers()readMethod.setAccessible(true); 136. tryoldValue=readMethod.invoke(this.object); 137. catch(Exceptionex)if138. (logger.isDebugEnabled()logger.debug(Couldnot 139. readpreviousvalueofproperty+ 140. this.nestedPath+propertyName+,ex); 141. valueToApply= 142. this.typeConverterDelegate.convertIfNecessary 143. (oldValue,originalValue,pd); 144. pv.getOriginalPropertyValue().conversionNecessary= 145. (valueToApply!=originalValue); 146. /这里取得注入属性的set方法,通过反射机制,把对象注入进去。 147. MethodwriteMethod=pd.getWriteMethod();if148. (!Modifier.isPublic(writeMethod.getDeclaringClass().getModif 149. iers()writeMethod.setAccessible(true); 150. writeMethod.invoke(this.object,valueToApply);catch151. (InvocationTargetExceptionex)PropertyChangeEvent 152. propertyChangeEvent=new153. PropertyChangeEvent(this.rootObject,this.nestedPath+ 154. propertyName,oldValue,pv.getValue();if155. (ex.getTargetException()instanceofClassCastException) 156. pd.getPropertyType(),ex.getTargetException();else157. thrownew158. MethodInvocationException(propertyChangeEvent, 159. ex.getTargetException();catch160. (IllegalArgumentExceptionex)PropertyChangeEventpce= 161. newPropertyChangeEvent(this.rootObject,this.nestedPath+ 162. propertyName,oldValue,pv.getValue();thrownew163. TypeMismatchException(pce,pd.getPropertyType(),ex); 164. catch(IllegalStateExceptionex)PropertyChangeEvent 165. pce=newPropertyChangeEvent(this.rootObject, 166. this.nestedPath+propertyName,oldValue, 167. pv.getValue();thrownew168. ConversionNotSupportedException(pce,pd.getPropertyType(), 169. ex);catch(IllegalAccessExceptionex) 170. PropertyChangeEventpce=new171. PropertyChangeEvent(this.rootObject,t

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论