注入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容器的相关接口方法就可以非常方便地让上层应用使用了。回到前面的关于水桶的例子到这里我们不但找到了水源而且成功地把水装到了水桶中 同时对水桶里的水完成了一系列的处理 比如消毒、煮沸……尽管还是水但经过一系列的处理以后这些水已经是开水了 已经可以直接饮用了

特网云(1050元),IP数5 个可用 IP (/29) ,美国高防御服务器 无视攻击

特网云特网云为您提供高速、稳定、安全、弹性的云计算服务计算、存储、监控、安全,完善的云产品满足您的一切所需,深耕云计算领域10余年;我们拥有前沿的核心技术,始终致力于为政府机构、企业组织和个人开发者提供稳定、安全、可靠、高性价比的云计算产品与服务。官方网站:https://www.56dr.com/ 10年老品牌 值得信赖 有需要的请联系======================特网云美国高防御...

欧路云:美国200G高防云-10元/月,香港云-15元/月,加拿大480G高防云-23元/月

欧路云 主要运行弹性云服务器,可自由定制配置,可选加拿大的480G超高防系列,也可以选择美国(200G高防)系列,也有速度直逼内地的香港CN2系列。所有配置都可以在下单的时候自行根据项目 需求来定制自由升级降级 (降级按天数配置费用 退款回预存款)。由专业人员提供一系列的技术支持!官方网站:https://www.oulucloud.com/云服务器(主机测评专属优惠)全场8折 优惠码:zhuji...

A2Hosting三年付$1.99/月,庆祝18周年/WordPress共享主机最高优惠81%/100GB SSD空间/无限流量

A2Hosting主机,A2Hosting怎么样?A2Hosting是UK2集团下属公司,成立于2003年的老牌国外主机商,产品包括虚拟主机、VPS和独立服务器等,数据中心提供包括美国、新加坡softlayer和荷兰三个地区机房。A2Hosting在国外是一家非常大非常有名气的终合型主机商,拥有几百万的客户,非常值得信赖,国外主机论坛对它家的虚拟主机评价非常不错,当前,A2Hosting主机庆祝1...

依赖注入为你推荐
中国联通话费查询请问中国联通如何查询剩余话费?网站运营我想成为网站运营的人我该学什么??彩信中心联通手机的彩信中心如何设置?硬盘人500G的硬盘容量是多少啊?bt封杀为什么现在网上许多BT下载都被封了?宕机何谓宕机?ios系统ios系统和安卓系统对比起来有什么优点和缺点?ios系统iOS系统是什么cr2格式如何打开CR2格式的照片呢gbk编码表GBK码表怎么查
ion 英语简历模板word 512m内存 好看的桌面背景图 权嘉云 河南m值兑换 cdn加速原理 1g内存 中国电信宽带测速网 支持外链的相册 google台湾 智能dns解析 云服务器比较 免费个人主页 阿里云手机官网 服务器托管价格 windows2008 gotoassist cc攻击 8度空间论坛 更多