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>

  • 相关阅读:
    ASSIC码对照表
    IIS注册 net环境
    Remoting1
    WinCE API
    【可下载】C#中关于zip压缩解压帮助类的封装
    【原创,提供下载】winfrom 打印表格,字符串的封装
    一个可编辑div中粘贴内容时过滤掉粘贴内容的一些特殊的样式或者标签
    限制一个文本框只能输入数字以及限制最大只能输入的数字
    文本框中有默认的文字,写获取焦点和失去焦点的文字显示与消失的效果
    鼠标滑过图片变大,移开还原大小的动画效果
  • 原文地址:https://www.cnblogs.com/wucg/p/2173252.html
Copyright © 2011-2022 走看看