在 Spring.NET 中使用 PropertyPlaceholderConfigurer 设置配置参数

首先,在 <configSections> 中增加了一个配置节的定义 databaseSettings,定义如下:

<section name="databaseSettings" type="System.Configuration.NameValueSectionHandler"/>


然后,在配置文件中定义了关于数据访问的一些配置参数,以备以后使用,每个配置参数通过 key 设置名字,value 是相应的值。

1 <databaseSettings>
2 <add key="db.datasource" value=".\SQLEXPRESS; Integrated Security=true; AttachDbFilename=|DataDirectory|northwnd.mdf; User Instance=true;"/>
3 <add key="db.user" value="springqa"/>
4 <add key="db.password" value="springqa"/>
5 <add key="db.database" value="Northwind"/>
6 </databaseSettings>


在 Spring.NEt 中,通过 PropertyPlaceholderConfigurer 读取配置参数,然后,可以在 Spring.NET 中使用

<!-- Property placeholder configurer for database settings -->
<object type="Spring.Objects.Factory.Config.PropertyPlaceholderConfigurer, Spring.Core">
<property name="ConfigSections" value="databaseSettings"/>
</object>


以后,在配置文件中,可以使用这些配置参数了。

<db:provider id="DbProvider" provider="SqlServer-1.1"
connectionString
="Server=${db.datasource};database=${db.database};uid=${db.user};pwd=${db.password};"/>

下面的例子,演示了如何使用这种方式。

在控制台程序的配置文件 app.config 中,首先使用 PropertyPlaceholderConfigurer 取得配置参数,然后,使用取得的参数来为
PropertyPlaceholderConfigurer_Sample.Demo 的属性 Username 赋值。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<!-- spring 配置节 -->
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>

<!-- 数据库的配置参数节 -->
<section name="databaseSettings" type="System.Configuration.NameValueSectionHandler"/>
</configSections>

<!-- 数据库的配置参数 -->
<databaseSettings>
<add key="db.datasource" value=".\SQLEXPRESS; Integrated Security=true; AttachDbFilename=|DataDirectory|northwnd.mdf; User Instance=true;"/>
<add key="db.user" value="springqa"/>
<add key="db.password" value="springqa"/>
<add key="db.database" value="Northwind"/>
</databaseSettings>
<spring>
<context>
<resource uri="config://spring/objects"/>
</context>
<objects>

<object type="Spring.Objects.Factory.Config.PropertyPlaceholderConfigurer, Spring.Core">
<property name="ConfigSections" value="databaseSettings"/>
</object>

<object id="demo" type="PropertyPlaceholderConfigurer_Sample.Demo">
<!-- 使用数据库配置参数 -->
<property name="Username" value="${db.user}"/>
</object>
</objects>

</spring>

</configuration>

在程序中,我们可以显示这个值。

namespace PropertyPlaceholderConfigurer_Sample
{
public class Demo
{
public string Username { get; set; }
}

class Program
{
static void Main(string[] args)
{
Spring.Context.IApplicationContext context
= Spring.Context.Support.ContextRegistry.GetContext();

Demo demo = context.GetObject("demo") as Demo;

Console.WriteLine(demo.Username);
}
}
}

如果项目是一个 Web 应用程序,那么,spring 的配置需要修改一下。使用 Spring.Context.Support.WebContextHandler, Spring.Web,它定义在 Spring.Web 中,可以直接配置 Spring.NET。

    <!-- spring 配置节 -->
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.WebContextHandler, Spring.Web"/>
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
原文地址:https://www.cnblogs.com/haogj/p/2198482.html