Add VariableSource configuration sections to documentation
This commit is contained in:
@@ -4996,24 +4996,259 @@ cfg.PostProcessObjectFactory(factory);</programlisting></para>
|
||||
</object>
|
||||
</list>
|
||||
</property>
|
||||
</object>
|
||||
</programlisting><note>
|
||||
</object> </programlisting><note>
|
||||
<para>The use of the <property>IgnoreMissingResources</property>
|
||||
property above will mean that if the property file is not found it
|
||||
will be silently ignored and the resolution will continue to
|
||||
<classname>ConfigSectionVariableSource</classname>.</para>
|
||||
</note>The IVariableSource interface is shown below</para>
|
||||
</note></para>
|
||||
|
||||
<programlisting language="csharp">public interface IVariableSource
|
||||
<para>The variable sources that Spring.NET provides out of the box are
|
||||
described in the following sections.</para>
|
||||
|
||||
<sect4>
|
||||
<title><literal>ConfigSectionVariableSource</literal></title>
|
||||
|
||||
<para>The <literal>ConfigSectionVariableSource</literal> allows you
|
||||
to define variables in a custom configuration section in you
|
||||
configuration file:</para>
|
||||
|
||||
<programlisting language="myxml"><!-- app.config: -->
|
||||
<configuration>
|
||||
<configSections>
|
||||
<section name="DonConfiguration" type="System.Configuration.NameValueSectionHandler"/>
|
||||
</configSections>
|
||||
<DonConfiguration>
|
||||
<add key="don_name" value="Dick Whitman"/>
|
||||
<add key="don_age" value="41" />
|
||||
</DonConfiguration>
|
||||
</configuration>
|
||||
|
||||
<!-- consume variables: -->
|
||||
<object type="Example.Person, Spring.IocQuickStart.VariableSources">
|
||||
<property name="Name" value="${don_name}" />
|
||||
<property name="Age" value="${don_age}" />
|
||||
</object>
|
||||
|
||||
<!-- VariableSource configuration: -->
|
||||
<object type="Spring.Objects.Factory.Config.ConfigSectionVariableSource, Spring.Core">
|
||||
<property name="SectionNames" value="DonConfiguration" />
|
||||
</object>
|
||||
</programlisting>
|
||||
|
||||
<para>This is similar to using the
|
||||
<literal>PropertyPlaceHolderConfigurer</literal> described
|
||||
above.</para>
|
||||
|
||||
<para>By simply configuring the appropriate section, you can use the
|
||||
<literal>ConfigSectionVariableSource</literal> to retrieve variables
|
||||
from application settings and user settings. Assuming your
|
||||
application's root namespace is
|
||||
<literal>Spring.IocQuickStart</literal>, then your application- and
|
||||
user settings can be loaded as variables by configuring the
|
||||
following variable sources:</para>
|
||||
|
||||
<programlisting language="myxml"><!-- From .net's ApplicationSettings: -->
|
||||
<object type="Spring.Objects.Factory.Config.ConfigSectionVariableSource, Spring.Core">
|
||||
<property name="SectionNames" value="applicationSettings/Spring.IocQuickStart.Properties.Settings" />
|
||||
</object>
|
||||
|
||||
<!-- From .net's UserSettings: -->
|
||||
<object type="Spring.Objects.Factory.Config.ConfigSectionVariableSource, Spring.Core">
|
||||
<property name="SectionNames" value="userSettings/Spring.IocQuickStart.Properties.Settings" />
|
||||
</object>
|
||||
</programlisting>
|
||||
|
||||
<para>If you configured your application settings as such:</para>
|
||||
|
||||
<para><mediaobject>
|
||||
<imageobject>
|
||||
<imagedata fileref="images/applicationsettings.png"></imagedata>
|
||||
</imageobject>
|
||||
</mediaobject>Then you can use <literal>${peggy_name}</literal>
|
||||
and <literal>${peter_age}</literal> as variable placeholders in your
|
||||
object definitions.</para>
|
||||
</sect4>
|
||||
|
||||
<sect4>
|
||||
<title><literal>PropertyFileVariableSource</literal></title>
|
||||
|
||||
<para>A <literal>PropertyFileVariableSource</literal> allows to read
|
||||
properties defined in a Java-style property file as variables.
|
||||
Assume a file named <literal>application.properties</literal> in
|
||||
your application folder containing:</para>
|
||||
|
||||
<programlisting>joan_name=Joan Harris
|
||||
joan_age=35</programlisting>
|
||||
|
||||
<para>You can use the <literal>${joan_name}</literal> and
|
||||
<literal>${joan_age}</literal> variable placeholders if you
|
||||
configure the following variable source:</para>
|
||||
|
||||
<programlisting language="myxml"><object type="Spring.Objects.Factory.Config.PropertyFileVariableSource, Spring.Core">
|
||||
<property name="Location" value="~\application.properties" />
|
||||
</object></programlisting>
|
||||
</sect4>
|
||||
|
||||
<sect4>
|
||||
<title><literal>ConfigurableVariableSource</literal></title>
|
||||
|
||||
<para>A <literal>ConfigurableVariableSource</literal> allows you
|
||||
define variables inline in a variable source definition. To
|
||||
configure the variables <literal>midge_name</literal> and
|
||||
<literal>midge_age</literal>, you can use the following
|
||||
<literal>ConfigurableVariableSource</literal> definition:</para>
|
||||
|
||||
<programlisting language="myxml"><object type="Spring.Objects.Factory.Config.ConfigurableVariableSource, Spring.Core">
|
||||
<property name="Variables">
|
||||
<name-values>
|
||||
<add key="midge_name" value="Midge Daniels"/>
|
||||
<add key="midge_age" value="33"/>
|
||||
</name-values>
|
||||
</property>
|
||||
</object></programlisting>
|
||||
</sect4>
|
||||
|
||||
<sect4>
|
||||
<title><literal>CommandLineArgsVariableSource</literal></title>
|
||||
|
||||
<para>You can use commandline arguments as a source for variables.
|
||||
Assume you issue the following command to start myapp:</para>
|
||||
|
||||
<para><programlisting>myapp /roger_name:"Roger Sterling" /roger_age:57</programlisting></para>
|
||||
|
||||
<para>The following variable source configuration allows you to use
|
||||
<literal>${roger_name}</literal> and <literal>${roger_age}</literal>
|
||||
as variable placeholders:</para>
|
||||
|
||||
<programlisting language="myxml"><object type="Spring.Objects.Factory.Config.CommandLineArgsVariableSource, Spring.Core">
|
||||
<property name="ArgumentPrefix" value ="/" /> <!-- optional; default: "/" -->
|
||||
<property name="ValueSeparator" value=":" /> <!-- optional; default: ":" -->
|
||||
</object></programlisting>
|
||||
</sect4>
|
||||
|
||||
<sect4>
|
||||
<title><literal>RegistryVariableSource</literal></title>
|
||||
|
||||
<para>Entries in the Windows registry can be used as variables. When
|
||||
your registry contains the key
|
||||
<literal>HKEY_CURRENT_USER\MyKey</literal> with entries
|
||||
<literal>freddy_name (String)</literal> and <literal>freddy_age
|
||||
(DWord)</literal>, then you can configure the following variable
|
||||
source to use <literal>freddy_name</literal> and
|
||||
<literal>freddy_age</literal> as variables:</para>
|
||||
|
||||
<programlisting language="myxml"><object type="Spring.Objects.Factory.Config.RegistryVariableSource, Spring.Core">
|
||||
<property name="Key" value="HKEY_CURRENT_USER\MyKey" />
|
||||
</object></programlisting>
|
||||
</sect4>
|
||||
|
||||
<sect4>
|
||||
<title><literal>EnvironmentVariableSource</literal></title>
|
||||
|
||||
<para>You can configure an
|
||||
<literal>EnvironmentVariableSource</literal> an to retrieve
|
||||
variables from environment variables available through <link
|
||||
ns6:href="http://msdn.microsoft.com/en-us/library/system.environment.aspx">.NET's
|
||||
<literal>System.Environment</literal> class</link>:</para>
|
||||
|
||||
<programlisting language="myxml"><object type="Spring.Objects.Factory.Config.EnvironmentVariableSource, Spring.Core" />
|
||||
</programlisting>
|
||||
|
||||
<para>To retrieve a value for a variable named
|
||||
<literal>ken_name</literal>, the
|
||||
<literal>EnvironmentVariableSource</literal> will directly call
|
||||
<literal>System.Environment.GetEnvironmentVariable("ken_name")</literal>.</para>
|
||||
</sect4>
|
||||
|
||||
<sect4>
|
||||
<title><literal>ConnectionStringsVariableSource</literal></title>
|
||||
|
||||
<para>Visual Studio has good support for configuring database
|
||||
connection strings in a <literal>connectionStrings</literal> section
|
||||
in you application configuration file. You can retrieve these
|
||||
connections as variables by configuring a
|
||||
<literal>ConnectionStringsVariableSource</literal>:</para>
|
||||
|
||||
<programlisting language="myxml"><object type="Spring.Objects.Factory.Config.ConnectionStringsVariableSource, Spring.Core" /></programlisting>
|
||||
|
||||
<para>Assume the following connection string section in your
|
||||
application configuration file:</para>
|
||||
|
||||
<programlisting language="myxml"><connectionStrings>
|
||||
<add name="myConnection"
|
||||
connectionString="Data Source=myserver;Integrated Security=True;..."
|
||||
providerName="System.Data.SqlClient" />
|
||||
</connectionStrings></programlisting>
|
||||
|
||||
<para>Then you would use the variables as in following object
|
||||
definition: </para>
|
||||
|
||||
<programlisting language="myxml"><object type="Example.MyClass, MyAssembly">
|
||||
<property name="ConnectionString" value="${myConnection.connectionString}" />
|
||||
<property name="ProviderName" value="${myConnection.providerName}" />
|
||||
</object> </programlisting>
|
||||
|
||||
<para><note>
|
||||
<para>Append ".connectionString" to the connection name to get
|
||||
the connection string and append ".providerName" to the
|
||||
connection name to get the provider name.</para>
|
||||
</note><note>
|
||||
<para>When adding a connection using Visual Studio's application
|
||||
settings, your connection will be named similar to
|
||||
<literal>MyNamespace.Properties.Settings.myConnection</literal>
|
||||
and the corresponding variable name for the connection string
|
||||
would become
|
||||
<literal>MyNamespace.Properties.Settings.myConnection.connectionString</literal>.
|
||||
</para>
|
||||
</note></para>
|
||||
</sect4>
|
||||
|
||||
<sect4>
|
||||
<title><literal>SpecialFolderVariableSource</literal></title>
|
||||
|
||||
<para>The <literal>SpecialFolderVariableSource</literal> resolves
|
||||
the full path for variable names against special folders as defined
|
||||
by the <literal>System.Environment.SpecialFolder</literal>
|
||||
enumeration. Add it to your variable sources as:</para>
|
||||
|
||||
<para><programlisting language="myxml"><object type="Spring.Objects.Factory.Config.SpecialFolderVariableSource, Spring.Core" /></programlisting></para>
|
||||
|
||||
<para>Now you can inject the full path to the current user's
|
||||
desktop, or to this machine's program files folders:</para>
|
||||
|
||||
<programlisting language="myxml"><object id="specials" type="Example.Specials, Spring.IocQuickStart.VariableSources">
|
||||
<property name="FullPathToDesktop" value="${Desktop}" />
|
||||
<property name="FullPathToPrgramFiles" value="${ProgramFiles}" />
|
||||
</object></programlisting>
|
||||
|
||||
<para>Any entry in the <link
|
||||
ns6:href="http://msdn.microsoft.com/library/system.environment.specialfolder.aspx">System.Environment.SpecialFolder</link>
|
||||
enumeration can be used as a variable name. </para>
|
||||
</sect4>
|
||||
|
||||
<sect4>
|
||||
<title>Custom <literal>IVariableSource</literal>
|
||||
implementations</title>
|
||||
|
||||
<para>The <literal>IVariableSource</literal> is the base interface
|
||||
for providing the ability to get the value of property placeholders
|
||||
(name-value) pairs from a variety of sources. The IVariableSource
|
||||
interface is shown below:</para>
|
||||
|
||||
<programlisting language="csharp">public interface IVariableSource
|
||||
{
|
||||
bool CanResolveVariable(string name);
|
||||
string ResolveVariable(string name);
|
||||
}</programlisting>
|
||||
|
||||
<para>This is a simple contract to implement if you should decide to
|
||||
create your own custom implemention. Look at the source code of the
|
||||
current implementations for some inspiration if you go that route. To
|
||||
register your own custom implemenation, simply configure
|
||||
VariablePlaceholderConfigurer to refer to your class.</para>
|
||||
<para>This is a simple contract to implement if you should decide to
|
||||
create your own custom implemention. Look at the source code of the
|
||||
current implementations for some inspiration if you go that route.
|
||||
To register your own custom implemenation, simply configure
|
||||
<literal>VariablePlaceholderConfigurer</literal> to refer to your
|
||||
class.</para>
|
||||
</sect4>
|
||||
</sect3>
|
||||
</sect2>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user