• 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>

  • 相关阅读:
    mysql 模糊查询LIKE 在tp中使用
    json字符串与 js对象互相转换
    1431. Kids With the Greatest Number of Candies
    1481. Least Number of Unique Integers after K Removals
    560. Subarray Sum Equals K
    1476. Subrectangle Queries
    1475. Final Prices With a Special Discount in a Shop
    网速和流量有什么关系
    计算网速的计算公式是什么
    php file_put_contents 函数的使用
  • 原文地址:https://www.cnblogs.com/wucg/p/2173252.html
Copyright © 2020-2023  润新知