(環(huán)境:.Net1.1 ,Spring.net 1.2 Preview)
1. 集合屬性注入
我們的組件經(jīng)常需要集合類型的屬性注入,比如我的Hook(鉤子)組件需要注入一個(gè)集合,該集合中的元素(int型)指明了要截獲哪些類型的消息。我們經(jīng)常使用IList處理集合問題:
當(dāng)IOC容器根據(jù)配置組裝后,發(fā)現(xiàn)HookList中的元素是string類型的,因?yàn)? IList中可以容納任意類型的object,所以Spring采用了配置的默認(rèn)類型--string。不出你想象,在運(yùn)行時(shí),如果使用下面的代碼一定會(huì)拋出異常:
foreach
(
int
key
in
this
.HookedList)
在經(jīng)過一番思索和試驗(yàn)后,結(jié)論是這樣的:
(1)如果要設(shè)置的集合屬性中的元素是string類型的,使用IList就很好。
(2)如果是其他類型,則屬性集合的類型最好是目標(biāo)類型的數(shù)組,即上面的屬性可以改為:
屬性的定義經(jīng)過修改后,配置文件和使用它的代碼不用做任何修改即可正常工作。
2.2006-06-12 當(dāng)Spring依據(jù)配置文件裝配對(duì)象時(shí),如果發(fā)生錯(cuò)誤,那么Spring在拋出異常之前將會(huì)依次調(diào)用每個(gè)已實(shí)例化對(duì)象的 Dispose 方法,然后再拋出System.Configuration.ConfigurationErrorsException。
如果你的程序在調(diào)用Spring.Context.Support.ContextRegistry.GetContext()時(shí)沒有了反應(yīng),那么很可能是在調(diào)用某個(gè)實(shí)例的Dispose方法中有ManualResetEvent.WaitOne(-1, true);的存在。
3.2006-06-13 從多個(gè)Xml配置文件中讀取對(duì)象
有三種類型的配置文件可以用來裝配組件:
(1)App.config 在Context配置節(jié)中作如下指示:
(2)嵌入到程序集中的xml配置文件。在Context配置節(jié)中作如下指示:
(3)AppBase下的普通xml配置文件。在Context配置節(jié)中作如下指示:
綜合起來:
比如objects.xml的內(nèi)容:
esbLogger對(duì)象定義于AppContext.xml中。
1. 集合屬性注入
我們的組件經(jīng)常需要集合類型的屬性注入,比如我的Hook(鉤子)組件需要注入一個(gè)集合,該集合中的元素(int型)指明了要截獲哪些類型的消息。我們經(jīng)常使用IList處理集合問題:
#region
HookList其中元素為整數(shù)類型
private IListhookList = new ArrayList();
public IListHookList
{
set
{
this .hookList = value;
}
}
#endregion
對(duì)應(yīng)的Spring配置片斷如下:
private IListhookList = new ArrayList();
public IListHookList
{
set
{
this .hookList = value;
}
}
#endregion
<propertyname="HookList">
<list>
<value>1</value>
<value>3</value>
<value>2</value>
</list>
</property>
<list>
<value>1</value>
<value>3</value>
<value>2</value>
</list>
</property>
當(dāng)IOC容器根據(jù)配置組裝后,發(fā)現(xiàn)HookList中的元素是string類型的,因?yàn)? IList中可以容納任意類型的object,所以Spring采用了配置的默認(rèn)類型--string。不出你想象,在運(yùn)行時(shí),如果使用下面的代碼一定會(huì)拋出異常:

在經(jīng)過一番思索和試驗(yàn)后,結(jié)論是這樣的:
(1)如果要設(shè)置的集合屬性中的元素是string類型的,使用IList就很好。
(2)如果是其他類型,則屬性集合的類型最好是目標(biāo)類型的數(shù)組,即上面的屬性可以改為:
#region
HookList
private int []hookList = new int [ 0 ];
public int []HookList
{
set
{
this .hookList = value;
}
}
#endregion
private int []hookList = new int [ 0 ];
public int []HookList
{
set
{
this .hookList = value;
}
}
#endregion
屬性的定義經(jīng)過修改后,配置文件和使用它的代碼不用做任何修改即可正常工作。
2.2006-06-12 當(dāng)Spring依據(jù)配置文件裝配對(duì)象時(shí),如果發(fā)生錯(cuò)誤,那么Spring在拋出異常之前將會(huì)依次調(diào)用每個(gè)已實(shí)例化對(duì)象的 Dispose 方法,然后再拋出System.Configuration.ConfigurationErrorsException。
如果你的程序在調(diào)用Spring.Context.Support.ContextRegistry.GetContext()時(shí)沒有了反應(yīng),那么很可能是在調(diào)用某個(gè)實(shí)例的Dispose方法中有ManualResetEvent.WaitOne(-1, true);的存在。
3.2006-06-13 從多個(gè)Xml配置文件中讀取對(duì)象
有三種類型的配置文件可以用來裝配組件:
(1)App.config 在Context配置節(jié)中作如下指示:
<
resourceuri
=
"
config://spring/objects
"
/>
(2)嵌入到程序集中的xml配置文件。在Context配置節(jié)中作如下指示:
<
resourceuri
=
"
assembly://SpringNestConfigTest/SpringNestConfigTest/AppContext.xml
"
/>
格式:uri
=
"
assembly://MyAssembly/MyProject/AppContext.xml
"
/
(3)AppBase下的普通xml配置文件。在Context配置節(jié)中作如下指示:
<
resourceuri
=
"
file://objects.xml
"
/>
綜合起來:
<
context
>
<!-- using section in App.config -->
< resourceuri = " config://spring/objects " />
<!-- using embeddedassemblyconfigurationfile -->
< resourceuri = " assembly://SpringNestConfigTest/SpringNestConfigTest/AppContext.xml " />
<!-- using commonconfigurationfile -->
< resourceuri = " file://objects.xml " />
</ context >
<!-- using section in App.config -->
< resourceuri = " config://spring/objects " />
<!-- using embeddedassemblyconfigurationfile -->
< resourceuri = " assembly://SpringNestConfigTest/SpringNestConfigTest/AppContext.xml " />
<!-- using commonconfigurationfile -->
< resourceuri = " file://objects.xml " />
</ context >
比如objects.xml的內(nèi)容:
<?
xmlversion
=
"
1.0
"
encoding
=
"
utf-8
"
?>
< objectsxmlns = " http://www.springframework.net " >
< object name = " esbNetMessageHook " type = " ESFramework.Network.EsbNetMessageHook,ESFramework " >
</ object >
< object name = " agileTcp " type = " ESFramework.Network.Tcp.AsynTcp,ESFramework " >
< propertyname = " EsbLogger " ref = " esbLogger " />
</ object >
</ objects >
< objectsxmlns = " http://www.springframework.net " >
< object name = " esbNetMessageHook " type = " ESFramework.Network.EsbNetMessageHook,ESFramework " >
</ object >
< object name = " agileTcp " type = " ESFramework.Network.Tcp.AsynTcp,ESFramework " >
< propertyname = " EsbLogger " ref = " esbLogger " />
</ object >
</ objects >
esbLogger對(duì)象定義于AppContext.xml中。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
