Spring.NET入门(Step1)——基本配置及对象创建

关于Spring.net及IoC、DI等的介绍,此处就不详述——偶不擅长啦。下面直入主题:首先说说基础配置。

首先是添加引用了,现在微软的nuget操作起来很方便了,直接在nuget找到Spring.Core的项,安装即可,至于其他的Spring.NET相关的dll可以需要时添加使用,如用AOP时,添加Spring.AOP。

下面就是创建对象了:

首先,可以使用最简单的例子,将所有配置都放在app.config中,类及配置文件见下方代码:

/// <summary>
/// 利用App.config配置创建对象
/// </summary>
public class AppConfigObject
{
    public AppConfigObject()
    {
    }

    public void Run()
    {
        Console.WriteLine("AppConfigObject运行!");
    }
}
<!--app.config配置文件-->
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
      <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
    </sectionGroup>
  </configSections>

  <spring>
    <context>
      <resource uri="config://spring/objects" />
    </context>

    <objects xmlns="http://www.springframework.net">
      <object id="AppConfigObject" type="SpringNETCreateObject.AppConfigObject, SpringNETCreateObject" />
    </objects>

  </spring>
</configuration>

使用时如下即可:

IApplicationContext ctx = ContextRegistry.GetContext();
AppConfigObject dao = ctx.GetObject("AppConfigObject") as AppConfigObject;
dao.Run();

但是,软件开发中,为了方便管理,往往添加单独的xml配置。

如:添加单独的Objects.xml

<?xml version="1.0" encoding="utf-8" ?>
<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 id="XMLObject" type="SpringNETCreateObject.XMLObject, SpringNETCreateObject">
  </object>
</objects>

然后再app.config的spring/context下添加

<!--文件复制到输出目录、生成操作为内容时使用file://-->
<resource uri="file://Objects.xml"/>

或者直接在代码里使用xml

IApplicationContext ctx1 = new XmlApplicationContext(
    "file://Objects.xml"
    );

对象的创建则使用ApplicationContext对象。

最后,讲一些特殊类型的对象创建配置。其中包括嵌入类型、泛型类型。

其中需要在app.config的spring/context下添加

<!--添加resource时,可以自行选择assembly或file,上面用过file了,这里用一下assembly-->
<!--使用assembly时,需要将生成操作修改为嵌入的资源-->
<resource uri="assembly://DAO/DAO/DAOObjects.xml"/>

objects配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<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 id="normaldao" type="DAO.NormalDAO,DAO"/>
 <!--嵌套类型需要使用‘+’连接-->
  <object id="nesteddao" type="DAO.NestedDAO+InnerDAO,DAO" />
 <!--泛型类型中的小于号需以‘&lt;’替代-->
  <object id="genericdao" type="DAO.GenericDAO&lt;int>,DAO" />
</objects>

相关类:

/// <summary>
/// 基类
/// </summary>
public interface BaseDAO
{
    void Run();
}

/// <summary>
/// 普通类型
/// </summary>
public class NormalDAO : BaseDAO
{
    public void Run()
    {
        Console.WriteLine("NormalDAO运行");
    }
}

/// <summary>
/// 嵌套类型
/// </summary>
public class NestedDAO
{
    public class InnerDAO : BaseDAO
    {
        public void Run()
        {
            Console.WriteLine("NestedDAO运行");
        }
    }
}

/// <summary>
/// 泛型类型
/// </summary>
/// <typeparam name="T"></typeparam>
public class GenericDAO<T> : BaseDAO
{
    public void Run()
    {
        Console.WriteLine("泛型({0})运行", this.GetType());
    }
}

使用:

BaseDAO normaldao = ctx.GetObject("normaldao") as BaseDAO;
normaldao.Run();

BaseDAO nesteddao = ctx.GetObject("nesteddao") as BaseDAO;
nesteddao.Run();

BaseDAO genericdao = ctx.GetObject("genericdao") as BaseDAO;
genericdao.Run();

项目地址:https://springnetstudy.codeplex.com

3 Replies to “Spring.NET入门(Step1)——基本配置及对象创建”

发表评论

邮箱地址不会被公开。 必填项已用*标注

*