Hiya
I would also like to specify other placeholder values in a file, but not [App].config, so the settings are accessible to all applications in the same directory. If the placeholder values can only be loaded from app.config, I have to duplicate the values in a number of app config files - one for each application in the directory.
You can have any number of arbitrary XML files for supplying name-value pairs. See the API documentation for the PropertyPlaceHolderConfigurer class.
You can supply any old resource location to the 'locations' property of the PropertyPlaceHolderConfigurer class. If your arbitrary name-value pairs are not in your [web]apps .config file, you will have to create a file like this (lets call it dao.config)...
Code:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="DaoConfiguration" type="System.Configuration.NameValueSectionHandler, System" />
</configSections>
<DaoConfiguration>
<add key="connection.string" value="dsn=MyDSN;uid=sa;pwd=myPassword;" />
</DaoConfiguration>
</configuration>
The corresponding .config file would look like this...
Code:
<objects xmlns="http://www.springframework.net"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd">
<object name="testObjectDao" type="Spring.Objects.TestObjectDAO, Spring.Core.Tests ">
<property name="maxResults" value="${maxResults}"/>
<property name="dbConnection" ref="myConnection"/
</object>
<object name="myConnection" type="System.Data.Odbc.OdbcConnection, System.Data">
<property name="connectionstring" value="${connection.string}"/>
</object>
<object name="appConfigPropertyHolder"
type="Spring.Objects.Factory.Config.PropertyPlaceholderConfigurer, Spring.Core">
<property name="locations">
<list>
<value>file://myApp/daoconfig.xml</value>
</list>
</property>
<property name="configSections">
<value>DaoConfiguration</value>
</property>
</object>
</objects>
If you have the full source codebase, you may wish to check out the unit tests for the relevant classes, which demonstrate how to use this feature. I will amend the reference documentation to include an example of this.
For example, I would like to replace '${machinename}' with the value of Environment.MachineName, and this would need some code to provide the replacement value dynamically.
Well, hopefully I'm not going slightly outfield on this response to your question, but environment variable expansion is supported out of the box on the PropertyPlaceHolderConfigurer class. See the API documentation for the above class, and consult the EnvironmentVariableMode property.
Code:
Is there any way for me to instruct Spring to replace placeholders from an instance of an object I provide (I am thinking of something implementing a hypothetical interface called IPlaceholderSource which might or might not exist in reality)?
Good point. I agree that the current implementation (which mandates the use of XML-based name-value pair configuration files) is poor. The class that sources the NameValueCollection should not be coupled to the class that consumes said NameValueCollection... its only done this way to be compliant with the Spring.Java codebase. I don't like this though, and I will change it... a JIRA issue has been pending for some weeks now.
Ciao
Rick