【Spring.net点滴】

    (环境:.Net1.1 ,Spring.net 1.2 Preview)

1.集合属性注入
    我们的组件经常需要集合类型的属性注入,比如我的Hook(钩子)组件需要注入一个集合,该集合中的元素(int型)指明了要截获哪些类型的消息。我们经常使用IList处理集合问题:

        #region HookList 其中元素为整数类型
        
private IList hookList = new ArrayList() ; 
        
public  IList HookList
        {
            
set
            {
                
this.hookList = value ;
            }
        }
        
#endregion

    对应的Spring配置片断如下:

                             <property name="HookList">
                                <list>
                                    <value>1</value>
                                    <value>3</value>
                                    <value>2</value>
                                </list>
                             </property>


    当IOC容器根据配置组装后,发现HookList中的元素是string类型的,因为IList中可以容纳任意类型的object,所以Spring采用了配置的默认类型--string。不出你想象,在运行时,如果使用下面的代码一定会抛出异常:

foreach(int key in this.HookedList)


    在经过一番思索和试验后,结论是这样的:
(1)如果要设置的集合属性中的元素是string类型的,使用IList就很好。
(2)如果是其他类型,则属性集合的类型最好是目标类型的数组,即上面的属性可以改为:

        #region HookList 
        
private int[] hookList = new int[0] ;
        
public  int[] HookList
        {
            
set
            {
                
this.hookList = value ;
            }
        }
        
#endregion


    属性的定义经过修改后,配置文件和使用它的代码不用做任何修改即可正常工作。

2.2006-06-12  当Spring依据配置文件装配对象时,如果发生错误,那么Spring在抛出异常之前将会依次调用每个已实例化对象的Dispose方法,然后再抛出System.Configuration.ConfigurationErrorsException。
    如果你的程序在调用Spring.Context.Support.ContextRegistry.GetContext()时没有了反应,那么很可能是在调用某个实例的Dispose方法中有ManualResetEvent.WaitOne(-1, true);的存在。

3.2006-06-13 从多个Xml配置文件中读取对象
    有三种类型的配置文件可以用来装配组件:
(1)App.config 在Context配置节中作如下指示:

<resource uri="config://spring/objects"/>


(2)嵌入到程序集中的xml配置文件。在Context配置节中作如下指示:

<resource uri="assembly://SpringNestConfigTest/SpringNestConfigTest/AppContext.xml"/>

    格式: uri="assembly://MyAssembly/MyProject/AppContext.xml"/

(3)AppBase下的普通xml配置文件。在Context配置节中作如下指示:

<resource uri="file://objects.xml"/>


综合起来:

        <context>
            
<!-- using section in App.config -->
            
<resource uri="config://spring/objects"/>            
            
<!-- using embedded assembly configuration file -->            
            
<resource uri="assembly://SpringNestConfigTest/SpringNestConfigTest/AppContext.xml"/>
            
<!-- using common configuration file -->  
            
<resource uri="file://objects.xml"/>
        
</context>


比如objects.xml的内容:

<?xml version="1.0" encoding="utf-8" ?> 
<objects xmlns="http://www.springframework.net">
    
<object name="esbNetMessageHook" type="ESFramework.Network.EsbNetMessageHook ,ESFramework">            
    
</object>
    
    
<object name="agileTcp" type="ESFramework.Network.Tcp.AsynTcp ,ESFramework">
        
<property name="EsbLogger" ref="esbLogger"/>
    
</object>
</objects>


    esbLogger对象定义于AppContext.xml中。

原文地址:https://www.cnblogs.com/zhuweisky/p/361535.html