在前面的俩篇中讲解了依赖注入的概念以及 Spring.NET 框架的核心模块介绍,今天就要看看怎么来使用 Spring.NET 实现一个简单的对象注入
常用文件
我们使用 Spring.Net 框架经常要使用到的是以下文件:
Common.Logging.dll 包含了 Spring.Net 日志方面的功能(必须)
Spring.Core.dll 包含了 Spring.Net 的核心库 (必须)
Spring.Data.dll 包含了 Spring.Net 的数据访问功能
Spring.Aop. dll 包含了 Spring.Net 对面向切面编程 (AOP) 的支持
Spring.web.dll 包含了 Spring.Net 对 ASP.NET 进行了一系列功能扩展
Spring.NET 常用接口
IObjectFactory 和 IApplicationContext
IObjectFactory 接口提供了一种高级配置机制,可以管理任何类型的对象,提供了配置框架和基本功能.
IApplicationContext 是 IObjectFactory 的子接口,表示 Spring IoC 容器,负责在应用程序中实例化,配置和组装许多对象.容器通过读取配置元数据获取对象的实例化,配置和组装.配置元数据以 XML 表示.它增加了与 Spring.NET 面向方面编程(AOP)功能,消息资源处理(用于国际化),事件传播和 Web 应用程序上下文(如 Web 应用程序上下文)在 Web 应用程序中使用的更容易的集成,IApplicationContext 增加了更多的企业特定功能. IApplicationContext 是 IObjectFactory 的一个完整的超集.
Spring.NET 提供了几个 IApplicationContext 接口的实现.在独立的应用程序中,通常在应用程序的 App.config 文件中以编程方式或声明方式创建 XmlApplicationContext 的实例.
在 Web 应用程序中,Spring 提供了一个 WebApplicationContext 实现,该实现通过在 Web.config 文件中添加自定义 HTTP 模块和 HTTP 处理程序来配置
下图是 Spring.NET 的工作原理图
如上图所示,Spring.NET IoC 容器使用一种形式的配置元数据; 这个配置元数据表示作为应用程序开发人员如何告诉 Spring 容器在应用程序中实例化,配置和组装对象.配置元数据以简单直观的 XML 格式提供
Spring.Net 的配置介绍
Spring.Net 的配置文件 (一般情况下加入到 App.Config 或者 Web.Config 中) 内容如下都是 XML 文件格式
配置文件中必须要要具有的节点
spring 结点
spring 结点 方式一
spring 结点 方式二
<spring>
<context type="Spring.Context.Support.XmlApplicationContext, Spring.Core">
<resource uri="file://objects.xml"/>
<resource uri="assembly://MyAssembly/MyProject/objects-dal-layer.xml"/>
</context>
</spring>
节点的 type 属性是可选的,在 Windows 应用中,其默认值就是 Spring.Context.Support.XmlApplicationContext,所以方式一和方式二完全相同
<spring>
<context>
<resource uri="file://objects.xml"/>
<resource uri="assembly://MyAssembly/MyProject/objects-dal-layer.xml"/>
</context>
</spring>
spring 和 context 节点的名称不是任意的,必须是 "spring" 和 "context",Spring.NET 本身将 "spring/context" 作为字符串常量定义在了 AbstractApplicationContext 类中以表示上下文的节点名称所以这个结点名称是不能随便改的,若要引用由以上配置创建的容器,可使用下面的代码:
IApplicationContext ctx = ContextRegistry.GetContext();
ContextRegistry 类既可用来初始化应用程序上下文,也可用来以服务定位器风格对容器中的对象进行访问,注意,使这一切成为可能的是 Spring.Context.Support.ContextHandler 类,该类实现了 FCL 的 IConfigurationSectionHandler 接口.必须在. NET 配置文件的节点中注册这个类,注册了这个节点处理器后,配置文件中的节点才能起作用,如下所示:
configSections 结点
源数据对象 xml 文件格式
<configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
</sectionGroup>
</configSections>
id 属性是用来标识单个对象定义的字符串. type 属性定义对象的类型,并使用完全限定的类型名称,包括程序集名称. id 属性的值是指协作对象.
<objects xmlns="http://www.springframework.net">
<object id="..." type="...">
<!-- collaborators and configuration for this object go here -->
</object>
<object id="...." type="...">
<!-- collaborators and configuration for this object go here -->
</object>
<!-- more object definitions go here -->
</objects>
实例介绍
根据配置文件格式我们配置一个 Person 对象
Person.cs
/*****************************************************
* ProjectName: Spring.NET01
* Description:
* ClassName: Person
* CLRVersion: 4.0.30319.42000
* Author: JiYF
* NameSpace: Spring.NET01
* MachineName: JIYF_PC
* CreateTime: 2018/1/14 11:59:12
* UpdatedTime: 2018/1/14 11:59:12
*****************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Spring.NET01
{
public class Person
{
public Person()
{ }
~Person()
{ }
public void print()
{
Console.WriteLine("我是一个Person对象");
}
}
}
App.config 文件
通过 Sprint.NET 容器注入对象
<?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"></resource>
</context>
<objects xmlns="http://www.springframework.net">
<!--一个person对象-->
<object id="person1" type="Spring.NET01.Person,Spring.NET01">
</object>
</objects>
</spring>
</configuration>
第一步引用 Spring.NET 动态库文件,这里只需要引用 Spring.Core.dll 和 Common.Logging.dll 动态库文件即可
代码测试:
执行结果:
class Program
{
static void Main(string[] args)
{
//普通对象创建
Person person = new Person();
person.print();
//通过Spring.NET ioc注入对象
IApplicationContext ctx = ContextRegistry.GetContext();
Person bennanhai = (Person)ctx.GetObject("person1");
bennanhai.print();
Console.Read();
}
}
这样一个简单的依赖注入例子就实现了
源代码工程下载
来源: http://www.bubuko.com/infodetail-2460924.html