1

在 Spring.NET 配置文件中使用设置(由 Visial Stidio 设置编辑器生成)是否有比使用 PropertyRetrievingFactoryObject 更好的方法:

  <object id="myUri" type="Spring.Objects.Factory.Config.PropertyRetrievingFactoryObject, Spring.Core">
    <property name="TargetObject">
      <object type="Properties.Settings, MyAssembly">
      </object>
    </property>
    <property name="TargetProperty" value="Default.MyUri" />
  </object>

  <object name="..." type="...">
    <property name="Uri">
      <ref object="myUri" />
    </property>
  </object>

?

对每种设置都这样做感觉不对...

4

1 回答 1

1

首先,为什么不把 url 放在 Spring.NET 配置文件中呢?有多种方式来配置您的应用程序可能会有点混乱。如果由于添加了 Web 引用而由 Visual Studio 生成了此文件,则应将 Web 引用的“Url 行为”属性从“动态”更改为“静态”。然后你可以删除所有 VS 生成的东西,App.config/Web.config 中的设置文件和配置代码。要配置您的代理,只需将其添加到容器中并使用 DI 注入 Url 属性。

无论如何,您可以使用 Spring Expression 语言实现您想要做的事情:

<object name="..." type="...">
  <property name="Uri" expression="T(Properties.Settings, MyAssembly).Default.MyUri">
</object>

另一种解决方案是使用带 IVariableSource 接口的 VariablePlaceholderConfigurer 组件: http ://www.springframework.net/doc-latest/reference/html/objects.html#objects-variablesource

<object type="Spring.Objects.Factory.Config.VariablePlaceholderConfigurer, Spring.Core">
   <property name="VariableSources">
      <list>
         <object type="MyCustomImplementationVariableSource, MyAssembly"/>
      </list>
   </property>
</object>

<object name="..." type="...">
  <property name="Uri" value="${MyUri}"/>
</object>

MyCustomImplementationVariableSource 是 IVariableSource 的实现,它将从您想要的位置(例如从您的 Settings 类)解析变量。

  • 布鲁诺
于 2009-07-31T12:29:20.213 回答