In the past, I used expression to achieve this, but through this question and answer of bbaia I found out that the best way to do this is to use a VariablePlaceholderConfigurer . When you use the VariablePlaceholderConfigurer instead of my “hack expression”, you are not attached to the appSettings / connectionStrings style configuration of your variables: you can switch to one of the VariableSources provided by spring.net or even implement your own IVariableSource .
Out of the box, spring.NET provides a VariablePlaceholderConfigurer to retrieve variables from standard .NET parameters such as AppSettings , ConnectionStrings , UserSettings and ApplicationSettings . This is partially illustrated by bbaia's answer, and you will find a complete example below.
Hacking Expressions: Calling ConfigurationManager from xml config
So, I do not advise you to use this, but this is the hack I used in the past, as applied to your configuration:
<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 the same approach for ConfigurationManager.AppSettings , for example:
<object object name="myService" type="com.acme.MyService, com.acme"> <constructor-arg name="AnotherConstructorArgument" expression="T(System.Configuration.ConfigurationManager).AppSettings['mySetting']" /> </object>
VariablePlaceholderConfigurer : .NET settings links from spring.NET xml config
You can easily configure VariablePlaceholderConfigurer to extract variables from standard .NET parameters such as AppSettings , ConnectionStrings , UserSettings and ApplicationSettings . For example, consider this xml configuration:
<?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"> <property name="SectionNames" value="appSettings,applicationSettings/q7991262.Properties.Settings,userSettings/q7991262.Properties.Settings" /> </object> </list> </property> </object> <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>
It reads the settings from this 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>
These configurations are taken from this working sample on github .