使用 Spring.NET 配置文件中的设置的更好方法?

发布于 2024-07-30 02:00:27 字数 654 浏览 4 评论 0原文

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

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

对每个设置都这样做感觉不太合适......

Is there a better way for using settings (generated by Visial Stidio Settings editor) in Spring.NET configuration file than using 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>

?

It does not feel right to do this for every setting...

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

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

发布评论

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

评论(1

故人如初 2024-08-06 02:00:27

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

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

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

另一个解决方案是将 VariablePlaceholderConfigurer 组件与 IVariableSource 接口结合使用:
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 类)解析变量。

  • 布鲁诺

First, why not just put the url in the Spring.NET configuration file ?
Having multiple ways to configure your application can be a bit confused.
If this file have been generated by Visual Studio because you added a Web Reference, you should change the 'Url Behavior' property of your Web references from 'Dynamic' to 'Static'.
then you can remove all the stuffs VS generates, the settings files and the configuration code in the App.config/Web.config.
To configure your proxy, just add it to the container and use DI to inject the Url property.

Anyway, you can achieve what you wanted to do with the Spring Expression language :

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

Another solution is to use the VariablePlaceholderConfigurer component with the IVariableSource interface :
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 is an implementation of the IVariableSource that will resolve variables from where you want (for example from your Settings class).

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