从Spring.NET xml配置访问ConfigurationManager.AppSettings值

时间:2011-11-03 07:03:02

标签: .net configuration connection-string spring.net configurationmanager

我有一个要求,要求我使用Spring.net来获取存储在app.config中的连接字符串,然后将检索到的连接字符串注入实例化的对象。

如何使用Spring.net的xml配置执行此操作?

例如,代替我的代码执行此操作:

// Spring.net config:
<object name="myService" type="com.acme.MyService, com.acme">
    <constructor-arg type="System.String" value="myConnectionName"/>
</object>

// Web.config:
<connectionStrings>
    <add name="myConnectionName" connectionString="DB_connectionstring"/>
</connectionStrings>
// Codes:
public class MyService {
    public MyService(string connectionName) {
        var connectionString = ConfigurationManager.AppSettings[connectionName];
        // use connectionString to create a DB connection, etc
    }
}

我想这样:

 // Spring.net config:
<object name="myService" type="com.acme.MyService, com.acme">
    <constructor-arg type="System.String" ref="retrievedConnectionString"/>
</object>    
// How to make a call similar to "ConfigurationManager.AppSettings[connectionName]" and get the connection string from Web.config and put inside "retrievedConnectionString"?

// Web.config:
<connectionStrings>
    <add name="myConnectionName" connectionString="DB_connectionstring"/>
</connectionStrings>
// Codes:
public class MyService {
    public MyService(string connectionString) {
        // use connectionString to create a DB connection, etc
    }
}

甚至可以从Spring.net xml config调用ConfigurationManager.AppSettings[..]吗?

2 个答案:

答案 0 :(得分:6)

过去我使用expression来实现这一目标,但通过这个问题和bbaia的回答,我发现更好的方法是使用VariablePlaceholderConfigurer。当您使用VariablePlaceholderConfigurer而不是我的“表达式黑客”时,您不会将自己绑定到变量的appSettings / connectionStrings样式配置:您可以切换到提供的VariableSources之一通过spring.net或甚至实现自己的IVariableSource

开箱即用,Spring.NET提供VariablePlaceholderConfigurer来从标准.NET设置中检索变量,如AppSettingsConnectionStringsUserSettings和{{ 1}}。 bbaia的答案部分说明了这一点,你将在下面找到一个完整的例子。

“表达黑客”:从xml config

调用ApplicationSettings

所以,我不建议你使用它,但这是我过去使用的hack,适用于你的配置:

ConfigurationManager

您可以对<object object name="myService" type="com.acme.MyService, com.acme"> <constructor-arg name="Connection" expression="T(System.Configuration.ConfigurationManager).ConnectionStrings['myConnectionName']" /> </object> 使用相同的方法,例如:

ConfigurationManager.AppSettings

<object object name="myService" type="com.acme.MyService, com.acme"> <constructor-arg name="AnotherConstructorArgument" expression="T(System.Configuration.ConfigurationManager).AppSettings['mySetting']" /> </object> :引用Spring.NET xml config

中的.NET设置

您可以轻松配置VariablePlaceholderConfigurer以从标准.NET设置中检索变量,例如VariablePlaceholderConfigurerAppSettingsConnectionStringsUserSettings。例如,考虑这个xml配置:

ApplicationSettings

它会从此<?xml version="1.0" encoding="utf-8"?> <objects xmlns="http://www.springframework.net" > <object type="Spring.Objects.Factory.Config.VariablePlaceholderConfigurer, Spring.Core"> <property name="VariableSources"> <list> <object type="Spring.Objects.Factory.Config.ConnectionStringsVariableSource, Spring.Core" /> <object type="Spring.Objects.Factory.Config.ConfigSectionVariableSource, Spring.Core"> <!-- Sections to read, sepearated by comma (leave out spaces) --> <property name="SectionNames" value="appSettings,applicationSettings/q7991262.Properties.Settings,userSettings/q7991262.Properties.Settings" /> </object> </list> </property> </object> <!-- Note that you have to append '.connectionstring' to the key! --> <object id="usingConnectionStringsVariableSource" type="q7991262.MyService, q7991262"> <property name="Connection" value="${myConnectionName.connectionString}" /> </object> <object id="configSectionVariableSource" type="q7991262.MyService, q7991262"> <property name="Connection" value="${myConnectionNameAppSettings}" /> </object> <object id="userSettingsSection" type="q7991262.MyService, q7991262"> <property name="Connection" value="${myConectionNameUserSetting}" /> </object> <object id="applicationSetting" type="q7991262.MyService, q7991262"> <property name="Connection" value="${myConectionNameApplicationSetting}" /> </object> </objects>

中读取设置
app.config

这些配置取自this working sample at github

答案 1 :(得分:3)

另一种解决方案是使用VariablePlaceholderConfigurer: http://www.springframework.net/doc-latest/reference/html/objects.html#objects-variablesource

ConnectionStringsVariableSource实现允许您从配置文件中的ConnectionString部分获取值。

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

<object name="myService" type="com.acme.MyService, com.acme">
    <constructor-arg type="System.String" value="${myConnectionName.connectionString}"/>
</object>

Spring.NET论坛的示例: http://forum.springframework.net/showthread.php?3961