过去我用过一个表达 http://www.springframework.net/doc-latest/reference/html/expressions.html为了实现这一点,但通过这个问题和 bbaia 的回答,我发现更好的方法是使用VariablePlaceholderConfigurer
。当您使用VariablePlaceholderConfigurer
而不是我的“表达黑客”,你不会把自己束缚在appSettings / connectionStrings
变量的样式配置:您可以切换到其中之一VariableSources http://www.springframework.net/doc-latest/reference/html/objects.html#objects-variablesource由 spring.net 提供,甚至实现您自己的IVariableSource
.
Spring.NET 提供开箱即用的VariablePlaceholderConfigurer
从标准 .NET 设置中检索变量,例如AppSettings
, ConnectionStrings
, UserSettings
and ApplicationSettings
。 bbaia 的答案部分说明了这一点,您将在下面找到完整的示例。
“表达式黑客”:调用ConfigurationManager
来自 xml 配置
所以,我不建议你使用这个,但这是我过去使用过的技巧,应用于你的配置:
<object object name="myService" type="com.acme.MyService, com.acme">
<constructor-arg name="Connection"
expression="T(System.Configuration.ConfigurationManager).ConnectionStrings['myConnectionName']" />
</object>
您可以使用相同的方法ConfigurationManager.AppSettings
, e.g.:
<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
and 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>
这些配置取自github 上的这个工作示例 https://github.com/serra/stackoverflow/tree/master/spring-questions/q7991262-accessing-configurationmanager.