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

2021年全新Vultr VPS主机开通云服务器和选择机房教程(附IP不通问题)

昨天有分享到"2021年Vultr新用户福利注册账户赠送50美元"文章,居然还有网友曾经没有注册过他家的账户,薅过他们家的羊毛。通过一阵折腾居然能注册到账户,但是对于如何开通云服务器稍微有点不对劲,对于新人来说确实有点疑惑。因为Vultr采用的是预付费充值方式,会在每月的一号扣费,当然我们账户需要存留余额或者我们采用自动扣费支付模式。把笔记中以前的文章推送给网友查看,他居然告诉我界面不同,看的不对...

RAKsmart推出7.59美元/月,云服务器产品Cloud Server,KVM架构1核1G内存40G硬盘1M带宽基础配置

近期RAKsmart上线云服务器Cloud Server产品,KVM架构1核1G内存40G硬盘1M带宽基础配置7.59美元/月!RAKsmart云服务器Cloud Server位于美国硅谷机房,下单可选DIY各项配置,VPC网络/经典网络,大陆优化/精品网线路,1-1000Mbps带宽,支持Linux或者Windows操作系统,提供Snap和Backup。RAKsmart机房是一家成立于2012年...

Sharktech:无限流量服务器丹佛,洛杉矶,荷兰$49/月起,1Gbps带宽哦!

鲨鱼机房(Sharktech)我们也叫它SK机房,是一家成立于2003年的老牌国外主机商,提供的产品包括独立服务器租用、VPS主机等,自营机房在美国洛杉矶、丹佛、芝加哥和荷兰阿姆斯特丹等,主打高防产品,独立服务器免费提供60Gbps/48Mpps攻击防御。机房提供1-10Gbps带宽不限流量服务器,最低丹佛/荷兰机房每月49美元起,洛杉矶机房最低59美元/月起。下面列出部分促销机型的配置信息。机房...

依赖注入为你推荐
行业关键词关键词有哪些分类?真正免费的网络电话有没有真正免费的网络电话?而且是好用的?快速美白好方法快速美白的好点子!?(不是晒黑的)ios7固件下载iOS的固件有正版盗版之分吗?我看到了蜂威网有iOS7的固件想下载试用一下,那里是测试版是正版吗创维云电视功能创维健康云电视有什么功能?ejb开发什么是EJB?2012年正月十五2012年正月十五上午9点27分出生的女孩儿五行缺什么,命怎么样安全漏洞计算机一般存在哪些安全漏洞?虚拟机软件下载那里可以下载虚拟系统,又该怎么安装呢??宽带接入服务器网络已连接,可无法连接到服务器为什么?网络已连接,可无法连接到服务
域名注册com 免费虚拟空间 国外服务器租用 荷兰vps 免费申请域名和空间 hostigation awardspace lunarpages siteground cpanel主机 海外服务器 空间打开慢 外国域名 tightvnc java空间 京东商城0元抢购 国外代理服务器软件 中国电信测速网 美国免费空间 空间技术网 更多