Spring.Net&NHibernate 学习 2
来源:互联网 发布:淘宝流量 编辑:程序博客网 时间:2024/04/29 15:07
IObjectFactory,IApplicationContext和IObjectDefinition接口介绍
IObjectFactory是初始化、配置及管理对象的实际容器(按:它是所有容器的父接口)。对象间通常会相互协作,我们也可以说它们相互间具有依赖性。这些依赖性可以通过IObjectFactory的配置数据反映出来。Spring.Objects.Factory.IObjectFactory接口有多个实现类。最常用的是Spring.Objects.Factory.Xml.XmlObjectFactory。Spring.NET框架的核心原则是非侵入性。简单的说,就是应用程序的代码不需要对Spring.NET的API有任何依赖。然而,如果要充分利用 IoC容器的功能(通过IObjectFactory或Spring.Context.IApplicationContext接口),有时候还必须要 获取这两个接口的引用。为此,可以在代码中使用new操作符来显式创建容器(按:即原文所说的IApplicationContext或 IObjectFactory,后文中,举凡涉及到含义为“管理对象的容器”而非特指接口的名称时,将原文中的IObjectFactory或 IApplicationContext称为“容器”或“IoC容器”);另外一种方式则更为简捷:在.NET应用程序的配置文件中用自定义配置节点来配 置容器。一旦容器建立,就不需要在代码中与之发生显式的互操作了(按:这个说法并不准确,比如对象的获取就必须通过 IApplicationContext,此处的含义指不需要进行任何与对象创建和管理相关的工作)。
下面分别是获取IObjectFactory,IApplictationContext的代码:
[C#]
IResource input = new FileSystemResource ("objects.xml");//IResource能以简单统一的方式访问许多可用System.IO.Stream表示的IO资源。
IObjectFactory factory = new XmlObjectFactory(input);
IApplicationContext context = new XmlApplicationContext(
"file://objects.xml",
"assembly://MyAssembly/MyProject/objects-dal-layer.xml");
//引用.NET程序集内嵌资源时的URI语法:assembly://<AssemblyName>/<NameSpace>/<ResourceName>
//要在VS中创建一个内嵌的资源,必须在文件属性编辑器中将xml配置文件的Build Action(生成操作)设为Embedded Resource(嵌入的资源)。
第一个Spring.NET的程序
n 建立项目
项目名称为:SpringSample,NameSpace为“OKEC.Sample.Spring”。
n 添加HelloTest类
HelloTest.cs
using System;
namespace OKEC.Sample.Spring
{
/// <summary>
/// HelloTest 的摘要说明。
/// </summary>
public class HelloTest
{
public HelloTest()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public void Test()
{
Console.WriteLine("This is Spring.NET Sample Test!");
Console.WriteLine("Please press Enter close the windows!");
Console.ReadLine();//让程序停留,回车关闭。
}
}
}
n 添加Spring.NET的配置文件
文件名:Spring_bean.xml,属性(生成操作)设置为:嵌入的资源/ Embedded Resource
<?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="Hello" type="OKEC.Sample.Spring.HelloTest,SpringSample" />
</objects>
n 建立Spring.NET的容器初始化对像
SpringContext.cs
using System;
using Spring.Core;
using Spring.Aop;
using System;
using Spring.Core;
using Spring.Aop;
using Spring.Context;
using Spring.Context.Support;
namespace OKEC.Sample.Spring
{
/// <summary>
/// SpringFactory 的摘要说明。
/// </summary>
public class SpringContext
{
public SpringContext()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
private static bool isInit = false;
private static IApplicationContext context;
public static void init()
{
string[] xmlFiles = new string[1];
xmlFiles[0] = "assembly://SpringSample/OKEC.Sample.Spring/Spring_bean.xml";
context = new XmlApplicationContext(xmlFiles);
isInit = true;
}
public static IApplicationContext Context
{
get{
if(!isInit)
{
init();
}
return context;
}
}
}
}
n 添加启动程序
StartMain.cs
using System;
namespace OKEC.Sample.Spring
{
/// <summary>
/// StartMain 的摘要说明。
/// </summary>
public class StartMain
{
public StartMain()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
[STAThread]
static void Main()
{
//Startup Spring Content
SpringContext.init();
//Test Spring IOC
HelloTest test = (HelloTest)SpringContext.Context.GetObject("Hello");
test.Test();
}
}
}
n 运行程序
结果为:
This is Spring.NET Sample Test!
Please press Enter close the windows!
你的第一个Spring.NET的程序成功了!
以上案例是从网上粘贴过来的,IApplicationContext是在程序中实例化的,下面做点修改,用多个配置文件创建容器(此处的配置文件是指包括了Spring.NET对象定义的XML文件,而非特指.config文件)创建方式是在标准.NET应用程序配置文件中(App.config或Web.config)添加自定义配置节点
App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="spring">
<section name="context"
type="Spring.Context.Support.ContextHandler, Spring.Core"/>
</sectionGroup>
</configSections>
<spring>
<context type="Spring.Context.Support.XmlApplicationContext,Spring.Core">>
<resource uri="assembly://SpringSample/OKEC.Sample.Spring/Spring_bean.xml"/>
</context>
</spring>
</configuration>
//注意配置中的<sectionGroup>节点,此处注册了一个类型为 Spring.Context.Support.ContextHandler的节点处理器,用来处理配置文件中的<context>节点, 该类实现了FCL中的IConfigurationSectionHandler接口。必须在.NET配置文件的< configSections>节点中注册这个类,否则Spring.NET无法自动处理应用程序上下文的配置信息,也无法使用 ContextRegistry对容器进行访问。
SpringContext中的
string[] xmlFiles = new string[1];
xmlFiles[0] = "assembly://SpringSample/OKEC.Sample.Spring/Spring_bean.xml";
context = new XmlApplicationContext(xmlFiles);
更改为: context = ContextRegistry.GetContext();
- Spring.Net&amp;amp;amp;amp;amp;NHibernate 学习
- Spring.Net&NHibernate 学习 2
- Spring.Net&NHibernate 学习 1
- Spring.Net&NHibernate 学习 3
- Spring.net&Nhibernate小结
- 学习Spring.NET、NHibernate
- Spring.Net&NHibernate 学习 4
- Spring.Net&NHibernate 学习 5
- Spring.NET 1.3.2 集成 NHibernate 3.2
- Spring.NET和NHibernate
- NHibernate+Spring.net配置
- spring.net+NHibernate+asp.net
- Spring.NET学习笔记二(NHibernate的配置)
- Spring.Net + NHibernate 入门例子
- Spring.Net + NHibernate 入门例子
- Spring.net+nHibernate的整合
- Spring.net + Nhibernate 框架 配置
- Spring.Net + NHibernate 入门例子
- 如何编写PHP扩展
- Scott Mitchell 的ASP.NET 2.0数据教程之十四:使用FormView 的模板
- B+树的组织结构
- xmlhttpRequest连接池
- RRDtool
- Spring.Net&NHibernate 学习 2
- SQL Server 如何重新恢复自动编号列的序号
- PHP创建多级目录并上传文件
- 项目经理必备——软件报价
- 发现自己真够懒的!
- rrdtool1.2.18安装笔记
- Scott Mitchell 的ASP.NET 2.0数据教程之十六::概述插入、更新和删除数据
- PHP正则表达式使用详解
- 策划设计制作网站应该注意的九大原则