Spring.Net Link to a dictionary item

I cannot find out how to use the element defined in the dictionary (xml spring config) elsewhere in the spring xml file. I tried something like this, but I ever get an error in init "obj1".

  <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>  
  <object name="obj1" type="MyTestClass" depends-on="Paths">
    <property name="cfg" expression="${Paths['cfgFile']}"/>
  </object>

Thank you for your help...

+3
source share
1 answer

My initial answer was accepted, but now I believe that the best answer would be:

Your expression is incorrect, it should be:

<object name="obj1" type="MyTestClass" depends-on="Paths">
  <property name="cfg" expression="@(Paths)['cfgFile']"/>
</object>

You can use expression syntax @(object-id-here) to extract an object from a Spring context using an expression .

Edit - below is the response that was accepted

, , app.config. , PropertyPlaceholderConfigurer; . 5.9.2.1.

:

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

myconfig.xml :

<!-- ... -->
<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>
<!-- ... -->

, app.config IResource.

cfgFile , value-ref (. Spring docs 5.3.2.4 , ). () , , ( ConfigurationObject).

+2

Source: https://habr.com/ru/post/1783365/


All Articles