1.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="assembly://FirstSpringNetApp/FirstSpringNetApp/Objects.xml"/>
<resource uri="config://spring/objects" />
</context>
<objects xmlns="http://www.springframework.net"/> <!--必要-->
</spring>
</configuration>
2.根据app.config设置的object.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="PersonDao" type="FirstSpringNetApp.PersonDao, FirstSpringNetApp" />
</objects>
3.persondao类
namespace FirstSpringNetApp
{
public class PersonDao
{
public override string ToString()
{
return "我是PersonDao";
}
}
}
4.对象的获取方式的说明 通常我们使用程序集路径
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spring.Context;
using Spring.Context.Support;
using Spring.Core.IO;
using Spring.Objects.Factory;
using Spring.Objects.Factory.Xml;
namespace FirstSpringNetApp
{
class Program
{
static void Main(string[] args)
{
AppRegistry();
XmlSystem();
Console.ReadLine();
}
static void AppRegistry()
{
IApplicationContext ctx = ContextRegistry.GetContext();
Console.WriteLine(ctx.GetObject("PersonDao").ToString());
}
static void XmlSystem()
{
string[] xmlFiles = new string[]
{
//"file://Objects.xml" //, 文件名
"assembly://FirstSpringNetApp/FirstSpringNetApp/Objects.xml" //程序集
};
IApplicationContext context = new XmlApplicationContext(xmlFiles);
IObjectFactory factory = (IObjectFactory)context;
Console.WriteLine(factory.GetObject("PersonDao").ToString());
}
static void FileSystem()
{
IResource input = new FileSystemResource(@"D:\Objects.xml"); //实际物理路径
IObjectFactory factory = new XmlObjectFactory(input);
Console.WriteLine(factory.GetObject("PersonDao").ToString());
}
}
}