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

rfchost:洛杉矶vps/双向CN2 GIA,1核/1G/10G SSD/500G流量/100Mbps/季付$23.9

rfchost怎么样?rfchost是一家开办了近六年的国人主机商,一般能挺过三年的国人商家,还是值得入手的,商家主要销售VPS,机房有美国洛杉矶/堪萨斯、中国香港,三年前本站分享过他家堪萨斯机房的套餐。目前rfchost商家的洛杉矶机房还是非常不错的,采用CN2优化线路,电信双程CN2 GIA,联通去程CN2 GIA,回程AS4837,移动走自己的直连线路,目前季付套餐还是比较划算的,有需要的可...

HostYun 新增美国三网CN2 GIA VPS主机 采用美国原生IP低至月15元

在之前几个月中也有陆续提到两次HostYun主机商,这个商家前身是我们可能有些网友熟悉的主机分享团队的,后来改名称的。目前这个品牌主营低价便宜VPS主机,这次有可以看到推出廉价版本的美国CN2 GIA VPS主机,月费地址15元,适合有需要入门级且需要便宜的用户。第一、廉价版美国CN2 GIA VPS主机方案我们可看到这个类型的VPS目前三网都走CN2 GIA网络,而且是原生IP。根据信息可能后续...

阿里云秋季促销活动 轻量云服务器2G5M配置新购年60元

已经有一段时间没有分享阿里云服务商的促销活动,主要原因在于他们以前的促销都仅限新用户,而且我们大部分人都已经有过账户基本上促销活动和我们无缘。即便老用户可选新产品购买,也是比较配置较高的,所以就懒得分享。这不看到有阿里云金秋活动,有不错的促销活动可以允许产品新购。即便我们是老用户,但是比如你没有购买过他们轻量服务器,也是可以享受优惠活动的。这次轻量服务器在金秋活动中力度折扣比较大,2G5M配置年付...

依赖注入为你推荐
雅特士中国最大的威客网是哪些?行业关键词关键词有哪些分类?刷网站权重刷出来的流量会提高网站的权重吗?windows优化大师怎么用windows优化大师怎么用啊?iphone越狱后怎么恢复苹果越狱后怎么恢复出厂设置qq怎么发邮件怎么发送QQ邮件vbscript教程vbs 学习方法以及 vbs 实例 有编程基础免费免费建站我想建一个自己的免费网站,但不知道那里有..rewritebase如何使用WampServer2.0软件微信电话本怎么用如何启用微信通讯录
域名注册查询 骨干网 东莞电信局 locvps bandwagonhost 圣迭戈 paypal认证 bash漏洞 suspended 表格样式 抢票工具 2017年黑色星期五 国外php空间 华为网络硬盘 智能骨干网 qingyun 129邮箱 国外代理服务器软件 吉林铁通 根服务器 更多