zoukankan      html  css  js  c++  java
  • Spring.net inject Dictionary.

    http://stackoverflow.com/questions/4591638/spring-net-ref-to-dictionary-item

    <object name="Paths" id="Paths" type="System.Collections.Generic.Dictionary&lt;string,string&gt;">
       
    <constructor-arg>
         
    <dictionary key-type="string" value-type="string">
           
    <entry key="cfgFile">
             
    <value>config.txt</value>
           
    </entry>
         
    </dictionary>
       
    </constructor-arg>
     
    </object>
    You can use the @(object-id-here) expression syntax to retrieve an object from the Spring context using an expression.
    <object name="obj1" type="MyTestClass" depends-on="Paths">
     
    <property name="cfg" expression="@(Paths)['cfgFile']"/>
    </object>

    Edit - below is the answer that was accepted

    I can imagine that you would like this kind of configuration to be available in your app.config. If so, you can use the PropertyPlaceholderConfigurer; see the example in section 5.9.2.1.

    In your case, it would look something like this:

    <!-- app.config -->
    <configuration>

     
    <configSections>
       
    <sectionGroup name="spring">
         
    <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
       
    </sectionGroup>
       
    <section name="PathConfiguration" type="System.Configuration.NameValueSectionHandler"/>
     
    </configSections>

     
    <PathConfiguration>
       
    <add key="cfgFile" value="config.txt"/>
       
    <add key="otherCfgFile" value="otherconfig.txt"/>
     
    </PathConfiguration>

     
    <spring>
       
    <context>
         
    <resource uri="mycongfig.xml"/>
       
    </context>
     
    </spring>

    </configuration>

    And your myconfig.xml contains:

    <!-- ... -->
    <object name="obj1" type="MyTestClass">
       
    <property name="cfg" value="${cfgFile}"/>
    </object>

    <object name="appConfigPropertyHolder"
           
    type="Spring.Objects.Factory.Config.PropertyPlaceholderConfigurer, Spring.Core">

       
    <property name="configSections">          
         
    <value>PathConfiguration</value>                              
       
    </property>              

    </object>
    <!-- ... -->

    Note that instead of app.config, you can use any other IResource.

    An alternative solution is to define the cfgFile as an object in your configuration and then in your dictionary reference this object using value-ref (see spring docs 5.3.2.4 on how to do this). But this (probably) isn't what you are looking for, since you're injecting primitive values (so it's not worth the effort of creating an explicit ConfigurationObject).

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    The elements of an OrderedDictionary are not sorted by the key, unlike the elements of a SortedDictionary<TKey, TValue> class. You can access elements either by the key or by the index

    spring注入的一些其它示例:

    from:

    http://www.cnblogs.com/Clingingboy/archive/2009/08/25/1553561.html

    集合属性(listsetname-values and dictionary)

    <property name="SomeList">
      <list>
        <value>a list element followed by a reference</value>
        <ref object="myConnection"/>
      </list>
    </property>
    <!--
      results in a call to the setter of the SomeDictionary (System.Collections.IDictionary) property
      -->
    <property name="SomeDictionary">
      <dictionary>
        <entry key="a string => string entry" value="just some string"/>
        <entry key-ref="myKeyObject" value-ref="myConnection"/>
      </dictionary>
    </property>
    <!--
      results in a call to the setter of the SomeNameValue (System.Collections.NameValueCollection) property
      -->
    <property name="SomeNameValue">
      <name-values>
        <add key="HarryPotter" value="The magic property"/>
        <add key="JerrySeinfeld" value="The funny (to Americans) property"/>
      </name-values>
    </property>
    <!-- 
      results in a call to the setter of the SomeSet (Spring.Collections.ISet) property 
      -->
    <property name="someSet">
      <set>
        <value>just some string</value>
        <ref object="myConnection"/>
      </set>
    </property>

  • 相关阅读:
    Windows系统创建符号链接文件
    msi软件包无法安装
    删除我的电脑里面软件快捷方式
    本地连接速度100.0mbps变10.0mbps如何恢复
    如何清理多余的Windows桌面右键菜单
    Windows免密码远程桌面
    Win8节省C盘空间攻略
    利用FTP将Linux文件备份到Windows
    Windows服务器之间rsync同步文件
    解决“Word无法访问您试图使用的功能所在的网络位置”问题
  • 原文地址:https://www.cnblogs.com/wucg/p/2173252.html
Copyright © 2011-2022 走看看