Using appSettings in your Spring configuration

This seems to be an undocumented feature that someone asked about on the user forum of the Spring.Net framework, I don’t know where I picked it up so I’m writing it down here.

When using .Net you tend to put configuration values in the appSettings section of your app or web.config. Entries like filepaths or other constants fit here nicely. If you’ve written an application powered by Spring.Net you’ll also have several spring configuration files which define the objects in use by your system. Most of their dependencies (if not all) are quite static but sometimes there’s the need for a configurable value. These tend to change more than other object relations so instead of searching through all your definitions to change them wouldn’t it be better to have a central place where you can manage them? Of course it would, so let’s use the appSettings section!

In order to reference the appSettings in your Spring configuration file you need to add this object definition to your configuration:

<object name="appConfigPropertyHolder" type="Spring.Objects.Factory.Config.PropertyPlaceholderConfigurer, Spring.Core">
  <property name="configSections">
    <value>appSettings</value>
  </property>
</object>

Your appSettings typically looks like this:

<appSettings>
  <add key="LoggerOutputFile" value="c:MyOutputFile.txt"/>
</appSettings>

Now you can reference this value using ${“LoggerOutputFile”} as illustrated below.

<object name="MyLogger" type="SpringAppSettingsConsole.Infrastructure.FileLogger, SpringAppSettingsConsole">
  <constructor-arg  value="${LoggerOutputFile}"/>
</object>

Full sample with source code can be downloaded here: SpringAppSettings.zip (247.31 kb)

Leave a Reply

Your email address will not be published. Required fields are marked *