过去,我曾使用,以达到这一目的,但通过这一问题和bbaia的回答,我发现,这样做的最好办法是使用。 VariablePlaceholderConfigurer
。 当你使用<代码>VariablePlace holderConfigurer而不是我的“expression-hack”,你不与appSettings /linkStrings
位位位位位位位位别:您可转向VariableSources /code>><<<<<<>>>>> 由春季提供。 页: 1
方框外,春季,NET提供<代码>可变PlaceholderConfigurer,以从标准中检索变量。 诸如AppSettings
,ConnectionStrings
,UserSettings
和ApplicationSettings
。 bbaia的回答部分说明了这一点,你举了一个完整的例子。
"Expression hack": calling ConfigurationManager
from xml config
因此,我不建议你这样做,但这是我过去使用的黑板,适用于你的配置:
<object object name="myService" type="com.acme.MyService, com.acme">
<constructor-arg name="Connection"
expression="T(System.Configuration.ConfigurationManager).ConnectionStrings[ myConnectionName ]" />
</object>
You can use this same approach for 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
: reference .NET settings from Spring.NET xml config
您可以轻而易举地配置一个<代码>可变的PlaceholderConfigurer,以便从标准中检索变量。 诸如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>
这些配置从。 该工作样本见。