Spring.Net 引用字典项

发布于 2024-10-10 20:56:02 字数 675 浏览 5 评论 0原文

我无法找到如何在 spring xml 文件中的其他位置使用字典(xml spring 配置)中定义的项目。我尝试了类似的方法,但在“obj1”的 init 中出现错误。

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

感谢您的帮助...

I can't find out how use an item defined in dictionary (xml spring config) at other place in the spring xml file. I tried something like this, but I ever get an error in init of "obj1".

  <object name="Paths" id="Paths" type="System.Collections.Generic.Dictionary<string,string>">
    <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>

Thanks for your help...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月隐月明月朦胧 2024-10-17 20:56:02

我原来的答案被接受了,但现在我相信更好的答案是:

你的表达不正确,应该是:

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

你可以使用 @(object-id-here) 表达式语法使用表达式从 Spring 上下文中检索对象

编辑 - 下面是已接受的答案

我可以想象您希望在 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>
<!-- ... -->

请注意,您可以使用任何其他 IResource< /代码>。

另一种解决方案是将 cfgFile 定义为配置中的对象,然后在字典中使用 value-ref 引用该对象(请参阅 spring 文档 5.3.2.4 了解如何执行此操作)。但这(可能)不是您正在寻找的,因为您正在注入原始值(因此不值得花费精力创建显式的 ConfigurationObject)。

My original answer was accepted, but now I believe a better answer would be:

Your expression is not correct, it should be:

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文