过去我使用expression 来实现这一点,但通过这个问题和bbaia 的回答,我发现更好的方法是使用VariablePlaceholderConfigurer。当您使用VariablePlaceholderConfigurer 而不是我的“表达式黑客”时,您不会将自己绑定到变量的appSettings / connectionStrings 样式配置:您可以切换到 spring.net 甚至提供的VariableSources 之一实现你自己的IVariableSource。
开箱即用,Spring.NET 提供 VariablePlaceholderConfigurers 来从标准 .NET 设置中检索变量,例如 AppSettings、ConnectionStrings、UserSettings 和 ApplicationSettings。 bbaia 的回答部分说明了这一点,您将在下面找到一个完整的示例。
“表达式破解”:从 xml 配置调用 ConfigurationManager
所以,我不建议您使用它,但这是我过去使用的 hack,应用于您的配置:
<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>
VariablePlaceholderConfigurer:参考 Spring.NET xml 配置中的 .NET 设置
您可以轻松配置 VariablePlaceholderConfigurer 以从标准 .NET 设置中检索变量,例如 AppSettings、ConnectionStrings、UserSettings 和 ApplicationSettings。例如,考虑这个 xml 配置:
<?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读取设置:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="q7991262.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="q7991262.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<connectionStrings>
<add name="myConnectionName"
connectionString="From connection string section."/>
</connectionStrings>
<appSettings>
<add key="myConnectionNameAppSettings"
value="From app setting section." />
</appSettings>
<userSettings>
<q7991262.Properties.Settings>
<setting name="myConectionNameUserSetting" serializeAs="String">
<value>My connection from user settings.</value>
</setting>
</q7991262.Properties.Settings>
</userSettings>
<applicationSettings>
<q7991262.Properties.Settings>
<setting name="myConectionNameApplicationSetting" serializeAs="String">
<value>My connection from application settings.</value>
</setting>
</q7991262.Properties.Settings>
</applicationSettings>
</configuration>
这些配置取自this working sample at github。